【问题标题】:Escaping quotation marks in PHP在 PHP 中转义引号
【发布时间】:2011-12-21 09:34:23
【问题描述】:

我收到一个解析错误,我认为这是因为"time" 上的引号引起的。我怎样才能让它把它当作一个完整的字符串?

<?php
    $text1 = 'From time to "time" this submerged or latent theater in 'Hamlet'
    becomes almost overt. It is close to the surface in Hamlet's pretense of madness,
    the "antic disposition" he puts on to protect himself and prevent his antagonists
    from plucking out the heart of his mystery. It is even closer to the surface when
    Hamlet enters his mother's room and holds up, side by side, the pictures of the
    two kings, Old Hamlet and Claudius, and proceeds to describe for her the true
    nature of the choice she has made, presenting truth by means of a show.
    Similarly, when he leaps into the open grave at Ophelia's funeral, ranting in
    high heroic terms, he is acting out for Laertes, and perhaps for himself as well,
    the folly of excessive, melodramatic expressions of grief.";

    $text2 = 'From time to "time"';

    similar_text($textl, $text2, $p);
    echo "Percent: $p%";

问题是我不能在每个引号之前手动添加\。这是我需要比较的实际文本。

【问题讨论】:

  • 此处为手册:php.net/manual/en/language.types.string.php - 转义与大多数其他 C 风格语言相同。
  • 实际上,上面截图中的问题始于单词 Hamlet 之前的单引号,正如着色所暗示的那样。由于字符串以单引号开头,所以里面的所有单引号都必须转义,除了最后一个。所以,第一个变量声明应该像这样开始:'From time to "time" this ... theater in \'Hamlet\'... $text2 变量其实没问题。

标签: php


【解决方案1】:

这样使用反斜杠

"From time to \"time\"";

反斜杠在 PHP 中用于转义引号内的特殊字符。由于PHP不区分字符串和字符,所以你也可以使用这个

'From time to "time"';

单引号和双引号的区别在于双引号允许字符串插值,这意味着您可以在字符串中内联引用变量,并且它们的值将像这样在字符串中进行评估

$name = 'Chris';
$greeting = "Hello my name is $name"; //equals "Hello my name is Chris"

根据您对问题的上次编辑,我认为您可以做的最简单的事情就是使用“heredoc”。它们不常用,老实说,我通常不会推荐它,但如果你想要一种快速的方法将这堵文本墙变成一个字符串。可以在此处找到语法:http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc,这是一个示例:

$someVar = "hello";
$someOtherVar = "goodbye";
$heredoc = <<<term
This is a long line of text that include variables such as $someVar
and additionally some other variable $someOtherVar. It also supports having
'single quotes' and "double quotes" without terminating the string itself.
heredocs have additional functionality that most likely falls outside
the scope of what you aim to accomplish.
term;

【讨论】:

    【解决方案2】:

    使用addslashes函数:

     $str = "Is your name O'Reilly?";
    
     // Outputs: Is your name O\'Reilly?
       echo addslashes($str);
    

    【讨论】:

    • 这很奇怪..我在使用完全相同的单词时遇到了麻烦:D。谢谢
    • 这应该是公认的答案,因为它对 php 硬编码文本和验证用户输入均有效。 +1
    【解决方案3】:

    使用htmlspecialchars()。然后引用和小于/大于符号不要破坏你的 HTML 标签~

    【讨论】:

      【解决方案4】:

      不要将您的文本保存在 PHP 文件中,而是保存在一个名为“text.txt”的普通文本文件中

      然后使用一个简单的$text1 = file_get_contents('text.txt'); 命令让您的文本没有任何问题。

      【讨论】:

      • 实际上,我将有一个从 doc 文件中返回文本的函数,例如 $text1 = readDoc("abc.docx"); // 返回文本 这会好吗?
      • 对不起,这是我的错误,我认为你可以在字符串开头之前使用@或其他字符,就像在 c# 中一样。无论如何它都有效。
      【解决方案5】:
      $text1= "From time to \"time\"";
      

      $text1= 'From time to "time"';
      

      【讨论】:

        【解决方案6】:

        要么转义引号:

        $text1= "From time to \"time\"";
        

        或者使用单引号来表示你的字符串:

        $text1= 'From time to "time"';
        

        【讨论】:

        • 问题是我有这个字符串。 '从时间到'时间',男孩会变得'困惑''它给出一个错误
        【解决方案7】:

        您可以使用 PHP 函数 addslashes() 对任何字符串使其兼容

        【讨论】:

        • 问题是我有一个带有许多“”标记的大文本,并且它给出了解析错误。我需要这个字符串,因为我将在similar_text() 函数中使用它
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-04-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多