2012-08-07 32 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中它们不是必需的,它也可能认为你要求它匹配那些。