0

我正在尝试使用 substr_count 从文本块中提取某些关键字组,并让它计算每个组中的单词出现的次数。

我创建了一个名为 $keywords 的数组,其中包含另一组数组,其中包含我正在寻找的实际关键字。

这是我当前的代码:

$textDump = "random bunch of text";

$keyWordsSports = array("nba", "raptor", "ufc", "basektball", "gym", "mma", "realgm", "running");
$keyWordsTech = array("apple", "rim", "blackberry", "facebook", "twitter", "google" );
$keywords = array($keyWordsSports, $keyWordsTech);
foreach ($keywords as $item){
    foreach ($item as $newItem){
        $number += substr_count(strtolower($textDump), strtolower($newItem));
        echo $number;
    };
};

我的问题是它计算所有数组中的所有关键字并将所有内容加在一起,我想要的只是每组关键字的总数。关于我应该做什么的任何想法?

4

3 回答 3

3
$keywords = array("sports"=>$keyWordsSports, "tech"=>$keyWordsTech);
$count=array("sports"=>0,"tech"=>0);
foreach ($keywords as $key=>$item){
    foreach ($item as $newItem){
        $count[$key] += substr_count(strtolower($textDump), strtolower($newItem));
    }
}
print_r($count);

编辑现场示例

于 2013-02-01T07:05:14.740 回答
1

试试这个 :

$textDump = "raptor bunch raptor basektball";

$keyWordsSports = array("nba", "raptor", "ufc", "basektball", "gym", "mma", "realgm", "running");
$keyWordsTech = array("apple", "rim", "blackberry", "facebook", "twitter", "google" );
$keywords = array($keyWordsSports, $keyWordsTech);
$matches=array();
foreach ($keywords as $item){
    foreach ($item as $newItem){
        $number = substr_count(strtolower($textDump), strtolower($newItem));
        if($number>0)
        {
        $matches[strtolower($newItem)]=$number;
        }
    };
};
print_r($matches);
于 2013-02-01T07:07:33.543 回答
0

你只需要 像这样echo$number外到内,foreach

$textDump = "random bunch of text";
$keyWordsSports = array("nba", "raptor", "ufc", "basektball", "gym", "mma", "realgm", "running");
$keyWordsTech = array("apple", "rim", "blackberry", "facebook", "twitter", "google" );
$keywords = array($keyWordsSports, $keyWordsTech);
foreach ($keywords as $item) {
    foreach ($item as $newItem) {
        $number += substr_count(strtolower($textDump), strtolower($newItem));
    }
    echo $number;
}
于 2013-02-01T07:31:27.917 回答