I need help breaking a Bible search string with PHP into variables or tokens. I'd like to get an explicit usage example of the solution offered in this post: PHP preg_match bible scripture format.
EDIT: the chapter and verses (from, to) are optional.
For example: I'd like to be able to split any of the 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'; // Where Exodus is the book, 1 the chapter number, 3 the starting verse and 7 the ending verse. [Book Chapter:StartVerse-EndVerse]
$pattern = '/[ :-]/';
list( $book, $chapter, $from, $to ) = preg_split($pattern, $string );
echo $book;
Allows me to get the nbook name: Exodus. I could also retrieve the chapter number the same way (echo $chapter), etc.
The problem I'm having with this solution is when the book name has more than one word.
Example '1 Samuel 3:4-5'. If I echo $book
for example, I get offset 3 not defined or a similar error.
It was suggested in the post linked above that this regex pattern is more complete:
/\w+\s?(\d{1,2})?(:\d{1,2})?([-–]\d{1,2})?(,\s\d{1,2}[-–]\d{1,2})?+$/
I guess my question is how to use this pattern or a similar one to split the search string as described above.
A similar issue is discussed here: PHP problems parsing a bible book string, but I'm just having some trouble modifying the pattern. I keep getting errors like : Undefined offset: 3 ...
I'd appreciate your help