0

我有这样的代码:

(请打开此链接)

    <?php
function stop_words($dok){
        $dokumen_trim = trim($dok);
        $dokumen_trim = strtolower($dokumen_trim);
        $hilangg = array("yang","di","dan","itu","dengan","untuk","tidak","ini","dari","dalam","akan","pada","juga","saya","ke","karena","tersebut","bisa","ada","mereka","lebih","sudah","atau","saat","oleh");
            $dokumen_trim = str_replace("/\b".$hilangg."\b/i", "", $dokumen_trim);

    return $dokumen_trim;
}
?>

但是,它不起作用..请帮帮我

4

1 回答 1

0

好的,首先您使用的是正则表达式 ( \b) 但用于str_replace替换您应该使用的位置preg_replace

其次 - 你不能只在其中粘贴一个数组。您需要创建一个正则表达式组:

第三,你什么都没有。这也包括您的单词边界(I am yang.将变成I am(没有点/空格)。

<?php
    function stop_words($dok){
        $dokumen_trim = trim($dok);
        $dokumen_trim = strtolower($dokumen_trim);
        $hilangg = array("yang","di","dan","itu","dengan","untuk","tidak","ini","dari","dalam","akan","pada","juga","saya","ke","karena","tersebut","bisa","ada","mereka","lebih","sudah","atau","saat","oleh");

        $regex = "/(\b)" . implode('|', array_map('preg_quote', $hilangg, '/')) . "(\b)/i";

        $dokumen_trim = preg_replace($regex, "$1$2", $dokumen_trim);

        return $dokumen_trim;
    }
?>
于 2013-10-09T13:30:45.470 回答