PHP problems parsing a bible book string

自古美人都是妖i 提交于 2019-12-24 04:43:07

问题


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


回答1:


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
)
     */



回答2:


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).




回答3:


$string = "Luke 1:26-38";
preg_match('#^(\w+)\s(\d+):(\d+)-(\d+)$#', $string, $result);
print_r($result);



回答4:


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;
}


来源:https://stackoverflow.com/questions/11574724/php-problems-parsing-a-bible-book-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!