我想匹配[space][slash][space]的任何实例。
即。
" / "
在正则表达式模式中。
我在任何地方都找不到答案。我错过了什么?
function madeby_remove_slash($text) {
preg_replace('/ '/ /', ' ', $text);
return $text;
}
echo madeby_remove_slash('This is the / text');
您
不会将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
''s/''s
使用这个工具,它很酷!
尝试从正则表达式中删除封闭的正斜杠。 AFAIK 它们在 PHP 中不是必需的,它很可能认为您也要求它匹配这些。