regex - PHP Parsing/explode Bible search string into variables or tokens -
i need breaking bible search string php variables or tokens. i'd explicit usage example of solution offered in post: php preg_match bible scripture format.
edit: chapter , verses (from, to) optional.
for example: i'd able split of following strings:
'john 14:16–17'; //book chapter:fromverse-toverse 'john 14:16'; //book chapter:fromverse 'john 14'; //book chapter 'john'; //book
the following:
<?php $string = 'exodus 1:3-7'; // exodus book, 1 chapter number, 3 starting verse , 7 ending verse. [book chapter:startverse-endverse] $pattern = '/[ :-]/'; list( $book, $chapter, $from, $to ) = preg_split($pattern, $string ); echo $book;
allows me nbook name: exodus. retrieve chapter number same way (echo $chapter), etc.
the problem i'm having solution when book name has more 1 word. example '1 samuel 3:4-5'. if echo $book
example, offset 3 not defined or similar error.
it suggested in post linked above regex pattern more complete:
/\w+\s?(\d{1,2})?(:\d{1,2})?([-–]\d{1,2})?(,\s\d{1,2}[-–]\d{1,2})?+$/
i guess question how use pattern or similar 1 split search string described above.
a similar issue discussed here: php problems parsing bible book string, i'm having trouble modifying pattern. keep getting errors : undefined offset: 3 ...
i'd appreciate help
i wouldn't 1 regex.
after reading bible-citation - common formats section in wikipedia, see bible-parser idea:
$holy_str = 'jonny 5:1,4-5,17,21;'; // split verses book chapters $parts = preg_split('/\s*:\s*/', trim($holy_str, " ;")); // init book $book = array('name' => "", 'chapter' => "", 'verses' => array()); // $part[0] = book + chapter, if isset $part[1] verses if(isset($parts[0])) { // 1.) chapter if(preg_match('/\d+\s*$/', $parts[0], $out)) { $book['chapter'] = rtrim($out[0]); } // 2.) book name $book['name'] = trim(preg_replace('/\d+\s*$/', "", $parts[0])); } // 3.) verses if(isset($parts[1])) { $book['verses'] = preg_split('~\s*,\s*~', $parts[1]); } print_r($book);
output (test @ eval.in):
array ( [name] => jonny [chapter] => 5 [verses] => array ( [0] => 1 [1] => 4-5 [2] => 17 [3] => 21 ) )
no matter here, if john 14:16
or 12 john: 3, 16-17
also see regex faq.
Comments
Post a Comment