合并两个数组后PHP关键字函数中断

时间:2012-02-13 00:17:13

标签: php mysql arrays keyword blacklist

此php函数检索字符串中使用的常用单词列表,并排除单词黑名单。

Array1:a,b,c

虽然默认黑名单很有用,但我需要从数据库中将单词添加到黑名单中。

Array2: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

1 个答案:

答案 0 :(得分:1)

如果数据库中的额外黑名单是从Windows客户端的管理面板中填充的,则每个单词的末尾可能会有一个迷路\ r \ n。因此,您的列表将是a,b,c,d \ r \ n,\ r \ n,\ n \ r \ n,\ n \ r \ n

尝试替换此行:

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

用这个:

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