开发者

substr_count and an array as a needle

开发者 https://www.devze.com 2023-01-15 11:28 出处:网络
How to use substr_count with an array as a needle. Like this: 开发者_StackOverflow中文版substr_count($str, array(\'find_this\', \'or_find_this\'));

How to use substr_count with an array as a needle. Like this:

开发者_StackOverflow中文版substr_count($str, array('find_this', 'or_find_this'));


You could use implode() to create a string of the array and create a regex kind of thing.

$array = array('find_this', 'or_find_this');
$string = implode('|', $array);
$count = count(preg_grep("/($string)/", $str));
echo $count;


You'll need to loop through them and add the substr_count() to the total count. A ready example is in the php manual page,

http://www.php.net/manual/en/function.substr-count.php#74952

0

精彩评论

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