-1

I'm using preg_replace function to remove a list of stop words. Currently, I have an array containing a lst of stop words. As the parameter for preg_replace I use this as the first argument (i.e, preg_replace(^$stopwordlist$, '',$string) As you can see I'm also using ^$ as I need to match word exactly.However I'm getting the following error

syntax error, unexpected '^', expecting ')' in 

Thanks

4

3 回答 3

1

如果$stopwordlist是一个数组,您可能首先想要implode()它。

至于语法错误,您需要将^and$放在引号中,您的正则表达式中也缺少分隔符。

将您的代码更改为以下内容:

// Implode with a |, which is basically an 'or' statement is regex
$pattern = '/^' . implode('|', $stopwordlist) . '$/';

// Replace them
$replaced = preg_replace($pattern, '', $string);

如果您需要一个地方来测试您的正则表达式,请尝试gskinner.com 的 RegExr

于 2013-08-28T06:46:16.113 回答
0

如果您想匹配确切的单词,REG EXPpreg_replace并不是您所需要的。

查看文档str_replacestrtr文档以找出您需要的文档。

http://www.php.net/manual/en/function.str-replace.php

http://www.php.net/manual/en/function.strtr.php

于 2013-08-28T06:44:56.617 回答
0

您可以使用

$string=str_replace($stopwordlist, "", $string);
于 2013-08-28T06:48:21.273 回答