【问题标题】:PHP Escape characterPHP 转义字符
【发布时间】:2013-03-13 12:53:23
【问题描述】:

我使用的是 PHP 环境,但在以转义字符形式显示数据时遇到问题。

示例:

如果我的字符串数据是\",我如何显示双引号"

同样,如果我的字符串数据是\\,我如何显示反斜杠\

【问题讨论】:

  • 在表单上显示数据时,您需要 HTML 转义,而不是 PHP
  • 我没有在 HTML 上显示数据,我提出这个问题是为了导出到 excel。

标签: php stringescapeutils


【解决方案1】:

要删除用作转义字符的\ 字符,请使用stripslashes 函数。

david@raston ~ $ cat tmp/test.php
<?php

$raw = 'double slash \\\\ escaped quote \"';
print $raw;
print "\n";
print stripslashes($raw);
?>

david@raston ~ $ php tmp/test.php
double slash \\ escaped quote \"
double slash \ escaped quote "

【讨论】:

    【解决方案2】:

    其他人都已经介绍过了,这都是documented here,但为了完整起见,我还建议看一下heredoc 表示法,当您的数据在多个上下文中发生变化时(例如。 PHP -> HTML/JS -> JS-Regex)

    <?php
    echo <<<__EOF
    It's not great for PHP indentation but often helps readability \ " ' `
    __EOF;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-09
      • 1970-01-01
      • 2017-12-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多