我有这样的代码:
(请打开此链接)
<?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;
}
?>
但是,它不起作用..请帮助我们
答案 0 :(得分: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;
}
?>