$comment = 'billie jean is not my lover she is just a girl';
$words = array('jean','lover','jean');
$lin = some_function_name($comment,$words);
($lin=3)
我尝试了 substr_count()
,但它对数组不起作用。是否有内置函数可以执行此操作?
最佳答案
我会使用 array_filter()。
这将适用于 PHP >= 5.3。对于较低版本,您需要以不同方式处理回调。
$lin = sum(array_filter($words, function($word) use ($comment) {return strpos($comment, $word) !== false;}));
关于php - 检查数组是否在字符串中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1676744/