1
    <?
    echo "Begin Function=";

    echo "<br>";

    $text = "2lyve: this is: 8475978474957845 948594: jfhdhfkd: just the 2lyve:          beginning:";
    function getTrends($text)
    {
        $subject = $text;
        $pattern ='/(\w+:)/Ui';
        preg_match_all($pattern, $subject, $matches);

        foreach($matches[1] as $value)
        {

            print $value."<br>";

        }

    }

    getTrends($text);
    ?>

结果将是:

Begin Function=

2lyve:

is:

948594:

jfhdhfkd:

2lyve:

beginning:

如何计算每个结果返回的次数并对其进行排名?另外,如何将这些结果导入 sql 数据库?

4

2 回答 2

1

PHP 实际上为此目的有一个特定的功能。

数组计数值

您的代码可以更改为

<?php
echo "Begin Function=";

echo "<br>";

$text = "2lyve: this is: 8475978474957845 948594: jfhdhfkd: just the 2lyve:          beginning:";
function getTrends($text)
{
    $subject = $text;
    $pattern ='/(\w+:)/Ui';
    preg_match_all($pattern, $subject, $matches);

    $findings = array_count_values($matches[1]);

    foreach($findings as $value=>$occ)
    {
        print $value."<br>";
    }

}

getTrends($text);
?>
于 2013-03-22T20:34:00.880 回答
0

在函数的开头声明一个数组$map = array();,然后代替

 print $value."<br>";

if(isset($map[$value])) {
    $map[$value]++;
} else {
    $map[$value] = 1;
}
于 2013-03-22T20:29:41.993 回答