How to check if a string contains a certain Chinese word in PHP? -
i got string $str = "颜色代码";
, check if string contain "颜色"
. i've tried using code below, maintain getting false return.
mb_strpos($str, "颜色", 0 ,"gbk");
the code work:
$str = "颜色代码"; $test = mb_strpos($str, "颜色", 0 ,"gbk"); echo $test;
but problem facing because 颜色
strpos
returns 0
right string position code logic might misinterpret false
. see mean take 颜色
, place @ end of string this:
$str = "代码颜色"; $test = mb_strpos($str, "颜色", 0 ,"gbk"); echo $test;
and returned string position 3 right well. improve approach see if 颜色
in string utilize preg_match
this:
$str = "颜色代码"; $test = preg_match("/颜色/", $str); echo $test;
and output boolean 1
equates true
believe looking for.
beyond functionality working expected, there clear speed benefit using preg_match
on mb_strpos
as shown here.
mb_strpos: 3.7908554077148e-5 preg_match: 1.1920928955078e-5
it’s more 3x faster utilize preg_match
when compared mb_strpos
.
php
No comments:
Post a Comment