是否有一个 PHP 函数只在双引号中添加斜杠而不是单引号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5611468/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 21:58:45  来源:igfitidea点击:

Is there a PHP function that only adds slashes to double quotes NOT single quotes

phpjson

提问by Tim

I am generating JSON with PHP.

我正在用 PHP 生成 JSON。

I have been using

我一直在使用

$string = 'This string has "double quotes"';

echo addslashes($string);

outputs: This string has \" double quotes\"

输出: This string has \" double quotes\"

Perfectly valid JSON

完全有效的 JSON

Unfortunately addslashes also escapes single quotes with catastrophic results for valid JSON

不幸的是,对于有效的 JSON,addslashes 也会转义单引号,并产生灾难性的结果

$string = "This string has 'single quotes'";

echo addslashes($string);

outputs: This string has \'single quotes\'

输出: This string has \'single quotes\'

In short, is there a way to only escape double quotes?

简而言之,有没有办法只转义双引号?

回答by Gumbo

Although you should use json_encodeif it's available to you, you could also use addcslashesto add \only to certain characters like:

尽管您应该使用json_encode它,如果它对您可用,您也可以使用仅addcslashes添加\到某些字符,例如:

addcslashes($str, '"\/')

You could also use a regular expression based replacement:

您还可以使用基于正则表达式的替换:

function json_string_encode($str) {
    $callback = function($match) {
        if ($match[0] === '\') {
            return $match[0];
        } else {
            $printable = array('"' => '"', '\' => '\', "\b" => 'b', "\f" => 'f', "\n" => 'n', "\r" => 'r', "\t" => 't');
            return isset($printable[$match[0]])
                   ? '\'.$printable[$match[0]]
                   : '\u'.strtoupper(current(unpack('H*', mb_convert_encoding($match[0], 'UCS-2BE', 'UTF-8'))));
        }
    };
    return '"' . preg_replace_callback('/\.|[^\x{20}-\x{21}\x{23}-\x{5B}\x{5D}-\x{10FFFF}/u', $callback, $str) . '"';
}

回答by hakre

Is there a PHP function that only adds slashes to double quotes NOT single quotes

是否有一个 PHP 函数只在双引号中添加斜杠而不是单引号

There is no function like addslashes()that only adds a slash to double quotes.

没有这样的函数addslashes()只在双引号中添加斜杠。

However you can make use of addcslashes()to only add slashes to specific characters, e.g. onlyto double quotes:

但是,您可以使用addcslashes()仅向特定字符添加斜杠,例如仅添加到双引号:

addcslashes($string, '"');

That does exactly as described. If you want to have it compatible with stripcslashes()however, you need to add the slash itself to the list of chars:

这与描述的完全一样。stripcslashes()但是,如果要使其兼容,则需要将斜杠本身添加到字符列表中:

addcslashes($string, '"\');

That should do the job you've been asking for. I have no idea if that is compatible with json encoding.

那应该可以完成您一直要求的工作。我不知道这是否与 json 编码兼容。

回答by Pascal MARTIN

If you are generating JSON, why not just use the json_encode()function ?

如果您正在生成 JSON,为什么不直接使用该json_encode()函数?

回答by Ryan Sutana

function json_string_encode( $str ) {
   $from = array('"');    // Array of values to replace
   $to = array('\"');    // Array of values to replace with

   // Replace the string passed
   return str_replace( $from, $to, $str );
}

To use the function you simply need to use

要使用该功能,您只需使用

$text = json_string_encode($text);