php preg_match regex split enty输入空格关键字到多个搜索

Actually I am doing the search in this way:

function myFun($data, $b){
    foreach ($b as $key){
        if(preg_match("/$data/i",$key->description)){
            echo 'found :'.$key->description;
        }
    }
}

if(isset($_GET["s"]) && !empty($_GET["s"]) ){
    myFun($_GET["s"], $b);
}

So if I search "er", I can match "lighter", "perimeter", "query" etc.

But If I would to search "er nd", I would like to do a multiple search to match for example:
"lighter", "query", "international", "bands", "bind", "sending"

How can I do it?

Replace the space in er nd by a pipe to obtain: er|nd,:

function myFun($data, $b) {

    // add this line
    $data = str_replace(' ', '|', $data);

    foreach ($b as $key){
        if(preg_match("/$data/i",$key->description)){
            echo 'found :'.$key->description;
        }
    }
}

have you tried? /$data$/i

$ for the end of the subject

You have two option:

  • do not search for "er nd". Split the string in "er" and "nd", then search them.
  • use regular expression "(er|nd)" (check PHP syntax for regexp)