我使用preg_replace
函數刪除停用詞的列表。目前,我有一個包含第一個停用詞的數組。至於preg_replace
參數我用這個作爲第一個參數(即preg_replace(^$stopwordlist$, '',$string)
正如你可以看到我還使用^$
,因爲我需要匹配字exactly.However我收到以下錯誤PHP中的preg_replace函數
syntax error, unexpected '^', expecting ')' in
謝謝
我使用preg_replace
函數刪除停用詞的列表。目前,我有一個包含第一個停用詞的數組。至於preg_replace
參數我用這個作爲第一個參數(即preg_replace(^$stopwordlist$, '',$string)
正如你可以看到我還使用^$
,因爲我需要匹配字exactly.However我收到以下錯誤PHP中的preg_replace函數
syntax error, unexpected '^', expecting ')' in
謝謝
如果你想匹配的原話,REG的EXP和preg_replace
是不是你所需要的。
給看看str_replace
或strtr
文檔,找到你需要的是哪一個。
如果$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。
您可以使用
$string=str_replace($stopwordlist, "", $string);
極品字符串,正則表達式的分隔符。 – elclanrs
請在這裏發佈您的代碼 – Alex
您是否學過PHP的基礎知識?請從[here]開始(http://php.net/manual/en/language.types.string.php) – HamZa