php preg_replace and the character


php preg_replace and the character

可能重复:
php preg_replace''

我正在尝试使用preg_replace将所有出现的"''"字符替换为"_c"。

以下是我尝试过的一些代码:

$outputStr=preg_replace('/'/','_c',$inputStr);
$outputStr=preg_replace('/''/','_c',$inputStr);

但是$outputStr在这两种情况下都以NULL结尾。获得"''"字符的正确正则表达式是什么?

您需要转义字符串和正则表达式中的斜杠:

若要在替换中使用反斜杠,必须将其加倍("''''" PHP字符串)。

请参阅http://de3.php.net/preg_replace.例如

$outputStr = preg_replace('/''''/','_c',$inputStr);

您只错过了一个'''':

$outputStr=preg_replace('/'''/','_c',$inputStr);