Strpos with exact matches

前端 未结 2 1872
醉酒成梦
醉酒成梦 2020-12-11 22:48

I have a function in php which I would like to perform a simple search on a string, using a kw as the search phrase, and return true if found.

This is what I have no

2条回答
  •  不思量自难忘°
    2020-12-11 23:30

    You can use word boundaries \b:

    if (preg_match("/\b".preg_quote($kw_to_search_for)."\b/i", $search_strings[$i])) {
        // found
    }
    

    For instance:

    echo preg_match("/\bProfessional\b/i", 'Prof'); // 0
    echo preg_match("/\bProf\b/i", 'Prof');         // 1
    

    /i modifier makes it case insensitive.

提交回复
热议问题