0

我试图从我最近的 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;
4

1 回答 1

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 )
于 2013-01-29T04:34:42.973 回答