[英]compare portion of the string using php
I want to check whether the search keyword 'cli' or 'ent' or 'cl' word exists in the string 'client' and case insensitive. 我想检查字符串“ client”中是否存在搜索关键字“ cli”或“ ent”或“ cl”,并且不区分大小写。 I used the preg_match function with the pattern '\\bclient\\b'. 我将preg_match函数与模式'\\ bclient \\ b'一起使用。 but it is not showing the correct result. 但是它没有显示正确的结果。 Match not found error getting. 找不到匹配项错误。
Please anyone help 请任何人帮助
Thanks 谢谢
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? 为什么不使用stripos()代替呢?
$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/
尝试使用模式/cli?|ent/
Explanation: 说明:
cli matches the first part. cli匹配第一部分。 The i? 我? makes the i optional in the search. 使i在搜索中成为可选项。
|
means or, and that matches cli
, or ent
. 表示或,并且与cli
或ent
匹配。
\\b
是单词边界,它与client
中的cli
不匹配,您需要删除\\b
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.