2012-08-07 70 views
1

我想匹配[空格] [斜槓] [空格]的任何實例。空間斜槓空間的正則表達式

即。

"/" 

在正則表達式模式。

我無法在任何地方找到答案。我錯過了什麼?

function madeby_remove_slash($text) { 
    preg_replace('/ \/ /', ' ', $text); 
    return $text; 
} 
echo madeby_remove_slash('This is the/text'); 

回答

3

您不會將preg_replace的返回值分配給函數中的$text變量。

function madeby_remove_slash($text) { 
    return preg_replace('/ \/ /', ' ', $text); // return what the preg_replace returns 
} 

,或者如果你要替換你可以使用str_replace過一個文本字符串。

str_replace('/', ' ', $text); // this works too 
+0

爲此而不是'return $ text',爲什麼不僅僅是'return preg_replace .....', – imm 2012-08-07 05:55:46

-1

嘗試從您的正則表達式中去除封閉的正斜槓。 AFAIK在PHP中它們不是必需的,它也可能認爲你要求它匹配那些。