简单的php regex使用preg_replace


Simple php regex using preg_replace

我想用奇数" " "更改字符串,例如:

<>之前他说:"我不这么认为。"之前

:

<>之前他说:"我不这么认为。"之前

我当前的代码是:

$sentence = addslashes(preg_replace('/^'&quot';$/',''"',$var));

我在代码中的问题是什么?

^$将只匹配整个字符串的开始和结束(或/m模式下的整行)。由于&quot;不像那样出现,您的正则表达式完全匹配它。只要去掉^$就可以了。

顺便说一句,也许你想用html_entity_decode()代替。

您最好使用PHPs htmlspecialchars_decode():

$var = "He said: &quot;I don't think so&quot;";
$sentence = htmlspecialchars_decode($var);

这个可以解决你的问题:

$yourstring = "He said: &quot;I don't think so&quot;";
$newstring = str_replace("&quot;","'"",$yourstring);
echo $newstring;