2012-02-13 254 views
0

此php函數檢索一個字符串中使用的常用單詞列表,並排除單詞的黑名單。合併兩個數組後合併PHP關鍵字函數

數組1:A,B,C

儘管默認黑名單是有用的,我需要的話從一個數據庫添加到黑名單。

數組2:d,E,F

我說,從我們的服務表中的字段獲得一個額外的列表MYSQL。 我爆炸\ n的話到一個數組併合並在函數開頭的兩個陣列使得黑名單現在是

ARRAY3:A,B,C,d,E,F

要測試我用print_r來顯示數組,並且它成功合併。

問題是這樣的......

如果我手動添加d,E,F爲默認陣列腳本返回字的清潔列表。 如果我將兩個數組合併成一個,它返回的單詞列表中仍然包含黑名單詞。

爲什麼合併後的數組與剛添加到默認數組不同?

下面是函數

function extractCommonWords($string,$init_blacklist){ 

    /// the default blacklist words 
    $stopWords = array('a','b','c'); 

    /// select the additional blacklist words from the database 
    $gettingblack_sql = "SELECT g_serv_blacklist FROM services WHERE g_serv_id='".$init_blacklist."' LIMIT 1"; 
    $gettingblack_result = mysql_query($gettingblack_sql) or die(mysql_error()); 
    $gettingblack_row = mysql_fetch_array($gettingblack_result); 
    $removingblack_array = explode("\n", $gettingblack_row["g_serv_blacklist"]); 

    // this adds the d,e,f array from the database to the default a,b,c blacklist 
    $stopWords = array_merge($stopWords,$removingblack_array); 

    // replace whitespace 
    $string = preg_replace('/\s\s+/i', '', $string); 
    $string = trim($string); 

    // only take alphanumerical chars, but keep the spaces and dashes too 
    $string = preg_replace('/[^a-zA-Z0-9 -]/', '', $string); 

    // make it lowercase 
    $string = strtolower($string); 

    preg_match_all('/\b.*?\b/i', $string, $matchWords); 
    $matchWords = $matchWords[0]; 

    foreach ($matchWords as $key => $item) { 
    if ($item == '' || in_array(strtolower($item), $stopWords) || strlen($item) <= 3){ 
    unset($matchWords[$key]);}} 

    $wordCountArr = array(); 

    if (is_array($matchWords)) { 
     foreach ($matchWords as $key => $val) { 
      $val = strtolower($val); 
      if (isset($wordCountArr[$val])) { 
       $wordCountArr[$val]++; 
      } else { 
       $wordCountArr[$val] = 1; 
      } 
     } 
    } 
    arsort($wordCountArr); 
    $wordCountArr = array_slice($wordCountArr, 0, 30); 
    return $wordCountArr; 
} 
/// end of function 



    /// posted string = a b c d e f g 
    $generate = $_POST["generate"]; 

    /// the unique id of the row to retrieve additional blacklist keywords from 
    $generate_id = $_POST["generate_id"]; 

    /// run the function by passing the text string and the id 
    $generate = extractCommonWords($generate, $generate_id); 

    /// update the database with the result 
    $update_data = "UPDATE services SET 
    g_serv_tags='".implode(',', array_keys($generate))."' 
    WHERE g_serv_acct='".$_SESSION["session_id"]."' 
    AND g_serv_id='".$generate_id."' LIMIT 1"; 
    $update_result = mysql_query($update_data); 
    if(!$update_result){die('Invalid query:' . mysql_error());} 
    else{echo str_replace(",",", ",implode(',', array_keys($generate)));} 
    /// end of database update 
+2

有一個合併數組和一個 「正常」 的陣列之間沒有區別。我想,數組合並不是你的問題。在這裏發佈'var_dump($ stopWords)',這樣我們就可以看到這段代碼的工作內容。 – deceze 2012-02-13 00:38:42

+0

該數組看起來很正常,如鍵> val。並按照其應該的方式返回給瀏覽器。 \ r是問題。感謝您的支持。 – Natrix 2012-02-13 05:01:37

回答

1

如果在數據庫中的額外的黑名單中從Windows客戶端的管理面板被填充,有可能是在每個單詞的末尾的寄生\ r 。因此,你的名單將是a,b,c,d \ r,e \ r,f \ r。

嘗試更換這一行:

$removingblack_array = explode("\n", $gettingblack_row["g_serv_blacklist"]); 

與此:

$removingblack_array = preg_split('/(\r|\n|\r\n)/', $gettingblack_row["g_serv_blacklist"]); 
+0

經過測試和完美! – Natrix 2012-02-13 03:47:57