简体   繁体   中英

how to search words or text from array using regular expression in PHP

I have array which contains different text and I want to search given text from array but I can not success

Here is my code

$search_word= "Joust Bag";
$data = array("Foo", "Joust Duffle Bag","Bag", "Wayfarer Messenger", "Voyage Yoga", "pushit Messagenger");

preg_match("/(.*)$search_word(.*)/", $data, $output_array);

echo print_r($output_array); // it should be return "Bag" and "Joust Bag"
\bso\b

Should only match so when it is on it's own:

if(preg_match('/\bso\b/',$str)){
    echo "Matches!";
}

As per example, if you want to search for "so" word then write it after \\b.

Note that preg_match returns 0 on no match and false on error so you may wish to check for these values. The above is also case insensitive. You can use /\\bso\\b/i to ignore case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM