开发者

compare portion of the string using php

开发者 https://www.devze.com 2022-12-18 15:49 出处:网络
I want to check whether the search keyword \'cli\' or \'ent\' or \'cl\' word exists in the string \'client\' and case insensitive. I used the preg_match function with the pattern \'\\bclient\\b\'. but

I want to check whether the search keyword 'cli' or 'ent' or 'cl' word exists in the string 'client' and case insensitive. I used the preg_match function with the pattern '\bclient\b'. but it is not showing the correct result. Match not found error getting.

Please anyone help

Th开发者_开发百科anks


I wouldn't use regular expressions for this, it's extra overhead and complexity where a regular string function would suffice. Why not go with stripos() instead?

$str = 'client';
$terms = array('cli','ent','cl');
foreach($terms as $t) {
    if (stripos($str,$t) !== false) {
        echo "$t exists in $str";
        break;
    }
}


Try the pattern /cli?|ent/

Explanation:

cli matches the first part. The i? makes the i optional in the search.

| means or, and that matches cli, or ent.


\b is word boundary, It would not match cli in client, you need to remove \b

0

精彩评论

暂无评论...
验证码 换一张
取 消