PHP 将 \" 和 \' 替换为 " 和 '

PHP Replacing \" and \' with " and '

我正在尝试遍历我从数据库中调出的数组字符串并过滤到可读状态。字符串可以有很多\'和\",下面只是一个例子。

$content = 'It\'s go to somewhere \"GREAT\"!';

我正在尝试使用 str_replace 但它不起作用...

$content1= str_replace('\\'', "'", $content );
$newcontent= str_replace('\\"', '"', $content1 );

输出应该是 它要去某个地方"GREAT"! 相反..我明白了 它要去某个地方\"GREAT\"!

我看了 preg_replace,但我不太明白 /.. 或从哪里开始。

请帮忙。

您要使用的是stripslashes($str)

Returns a string with backslashes stripped off. (\' becomes ' and so on.) Double backslashes (\) are made into a single backslash ().

$str = "Is your name O\'reilly?";

// Outputs: Is your name O'reilly?
echo stripslashes($str);

方法如下

            $content = 'It\'s go to somewhere \"GREAT\"!';
            $content = stripslashes($content);
            echo $content;