2013-03-22 45 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