2013-03-22 40 views
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); 
    ?> 

结果将是:Preg_match_all计数器?

Begin Function= 

2lyve: 

is: 

948594: 

jfhdhfkd: 

2lyve: 

beginning: 

如何指望有多少次返回每个结果和排名吗?另外,如何将这些结果导入到sql数据库中?

回答

1

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

array_count_values

您的代码可以改成

<?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); 
?> 
0

声明数组$map = array();在函数的开始,然后在

print $value."<br>"; 

的地方放

if(isset($map[$value])) { 
    $map[$value]++; 
} else { 
    $map[$value] = 1; 
} 
+0

感谢。我也调整了一下,打印出一个干净的格式,但我仍然试图按发生次数对它们进行排名 – ImJeaniusDoe 2013-03-26 16:15:49