【问题标题】:How to parse characters in a single-quoted string?如何解析单引号字符串中的字符?
【发布时间】:2023-03-03 23:42:01
【问题描述】:

要正确解析双引号字符串(我无法更改),我必须执行以下操作:

$string = '15 Rose Avenue\n Irlam\n Manchester'; 
$string = str_replace('\n', "\n", $string);
print nl2br($string); // demonstrates that the \n's are now linebreak characters

到目前为止,一切都很好。 但在我给定的字符串中,有\xC3\xA4 之类的字符。有很多这样的字符(以\x..开头) 如何使用换行符正确解析它们,如上所示?

【问题讨论】:

    标签: php hex quotes


    【解决方案1】:

    你可以使用

    $str = stripcslashes($str);
    

    【讨论】:

    • 不错。最好也提供一个到 php.net/manual/en/function.stripcslashes.php 的链接,并提到这里所有的 '\xYY' 都是十六进制字符表示法,该方法还可以处理类似 C 的 \n、\r ... 和八进制表示。
    【解决方案2】:

    您可以在单引号中转义 \

    $string = str_replace('\\n', "\n", $string);
    

    但如果你需要做\\xC3 等,你将有很多潜在的替换......最好使用带有函数(回调)的preg_replace_callback() 将它们转换为字节

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-19
      • 1970-01-01
      • 2016-04-06
      • 1970-01-01
      • 2020-07-26
      相关资源
      最近更新 更多