如何将多个字符串传递给substr_count?

时间:2013-01-29 04:19:58

标签: php arrays

我试图从我最近的200条推文中提取一堆关键字但到目前为止使用'substr_count'只允许我一次搜索一个字符串。

有没有办法定义关键字数组并使用'substr_count'为我计算所有这些关键字?

$recentTweets = $connection->get('https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=username');
GLOBAL $textDump;
for ($i=0; $i<count($recentTweets); $i++){
    $textDump .= $recentTweets[$i]->text . " ";
};
$keyWords = "the";
$number = substr_count(strtolower($textDump), strtolower($keyWords)); 
echo $number;

1 个答案:

答案 0 :(得分:1)

$textDump = "The quick brown fox jumps over the lazy dog";
$keyWords = array("the", "fox");
foreach ($keyWords as $keyWord) {
    $number[$keyWord] = substr_count(strtolower($textDump), strtolower($keyWord));
}
print_r($number); // Array ( [the] => 2 [fox] => 1 )