2013-08-28 60 views
-1

我使用preg_replace函數刪除停用詞的列表。目前,我有一個包含第一個停用詞的數組。至於preg_replace參數我用這個作爲第一個參數(即preg_replace(^$stopwordlist$, '',$string)正如你可以看到我還使用^$,因爲我需要匹配字exactly.However我收到以下錯誤PHP中的preg_replace函數

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

謝謝

+2

極品字符串,正則表達式的分隔符。 – elclanrs

+0

請在這裏發佈您的代碼 – Alex

+2

您是否學過PHP的基礎知識?請從[here]開始(http://php.net/manual/en/language.types.string.php) – HamZa

回答

1

如果$stopwordlist是你可能希望先implode()它的陣列。

至於語法錯誤,您需要將^$加引號引起來,你的正則表達式中也缺少分隔符。

更改您的代碼是這樣的:

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

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

如果你需要一個地方來測試你的正則表達式,嘗試gskinner.com's RegExr

0

您可以使用

$string=str_replace($stopwordlist, "", $string);