2

I hope you can help me.

I have a string like the following

Luke 1:26-38

And I would like to be able to break it up into tokens or individual variables so that I can use the variables in an SQL query.

I've tried using explode, however I've only been able to make it explode on one character such as : or -

My string has : and - and also a space between the name and the first number.

My goal is to have:

    $name = Luke;
    $book = 1;
    $from = 26;  
    $to = 38;

Is anyone able to help please.

Many thanks

hakre
  • 178,314
  • 47
  • 389
  • 754

4 Answers4

8

You can do that with a simple string scanning (Demo):

$r = sscanf("Luke 1:26-38", "%s %d:%d-%d", $name, $book, $from, $to);

The varibales then contain the information. %s represents a string (without spaces), %d a decimal. See sscanf.


To make this "bible safe", it needs some additional modifications:

$r = sscanf($string, "%[ a-zA-Z] %d:%d-%d", $name, $book, $from, $to);
$name = trim($name);

(Second demo).

hakre
  • 178,314
  • 47
  • 389
  • 754
  • I like this one better, was thinking too much in terms of what I would do in javascript – Esailija Jul 20 '12 at 07:31
  • You will need to make amends to this code in order to support the numbered books of the bible (i.e. I Timothy, II Timothy, etc). – worsnupd Jul 20 '12 at 07:36
  • @worsnupd: Right, but which is the name of that roman numeral in front for the variable? – hakre Jul 20 '12 at 07:39
  • It's part of the name of the book. It should end up inside the `$name` variable as well. – worsnupd Jul 20 '12 at 07:45
3
list( $name, $book, $from, $to ) = preg_split( '/[ :-]/', 'Luke 1:26-38' );

echo $name; //"Luke"

    /* Split results in an Array
(
    [0] => Luke
    [1] => 1
    [2] => 26
    [3] => 38
)
     */
Esailija
  • 130,716
  • 22
  • 250
  • 308
2
$string = "Luke 1:26-38";
preg_match('#^(\w+)\s(\d+):(\d+)-(\d+)$#', $string, $result);
print_r($result);
Sergii Stotskyi
  • 4,324
  • 1
  • 18
  • 19
0

regex is hard to configure for this because of the multiple configurations of bible book names, chapter and verse num. Because some books begin with a number and some books have multiple spaces in the book names.

I came up with this for building a sql query, it works for these passage search types.. (John), (John 3), (Joh 3:16), (1 Thes 1:1)

Book names can be 3 letter abbreviations.

Does unlimited individual word search and exact phrase.

$string = $_GET['sstring'];
$type = $_GET['stype'];
switch ($type){ 
    case "passage":
        $book = "";
        $chap = "";
        $stringarray = explode(':', $string); // Split string at verse refrence/s, if exist.
        $vref = $stringarray[1]; 
        $vrefsplit = explode('-', $vref);// Split verse refrence range, if exist.
        $minv = $vrefsplit[0];
        $maxv = $vrefsplit[1]; // Assign min/max verses.
        $bc = explode(" ", $stringarray[0]); // Split book string into array with space as delimiter.
        if(is_numeric($bc[count($bc)-1])){ // If last book array element is numeric?
            $chap = array_pop($bc); // Remove it from array and assign it to chapter.
            $book = implode(" ", $bc); // Put remaining elemts back into string and assign to book.
        }else{
            $book = implode(" ", $bc); // Else book array is just book, convert back to string.
        }
        // Build the sql query.
        $query_rs1 = "SELECT * FROM kjvbible WHERE bookname LIKE '$book%'";
        if($chap != ""){
            $query_rs1.= " AND chapternum='$chap'";
        }
        if($maxv != ""){
            $query_rs1.= " AND versenum BETWEEN '$minv' AND '$maxv'";
        }else if($minv != ""){
            $query_rs1.= " AND versenum='$minv'";
        }
    break;
    case "words":
        $stringarray = explode(" ", $string); // Split string into array.<br />
        // Build the sql query.
        $query_rs1 = "SELECT * FROM kjvbible WHERE versetext REGEXP '[[:<:]]". $stringarray[0] ."[[:>:]]'";
        if(count($stringarray)>1){
            for($i=1;$i<count($stringarray);$i++){
                $query_rs1.= " AND versetext REGEXP '[[:<:]]". $stringarray[$i] ."[[:>:]]'";
            }
        }
    break;
    case "xphrase":
        // Build the sql query.
        $query_rs1 = "SELECT * FROM kjvbible WHERE versetext REGEXP '[[:<:]]". $string ."[[:>:]]'";
    break;
    default :
    break;
}