在 PHP 中转义引号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7999148/
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-26 03:44:48  来源:igfitidea点击:

Escaping quotation marks in PHP

php

提问by user478636

I am getting a parse error, and I think it's because of the quotation marks over "time". How can I make it treat it as a whole string?

我收到一个解析错误,我认为这是因为"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%";

The problem is that I can't manually add \before every quotation mark. This is the actual text I need to compare.

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

回答by MoarCodePlz

Use a backslash as such

像这样使用反斜杠

"From time to \"time\"";

Backslashes are used in PHP to escape special characters within quotes. As PHP does not distinguish between strings and characters, you could also use this

PHP 中使用反斜杠来转义引号内的特殊字符。由于 PHP 不区分字符串和字符,因此您也可以使用它

'From time to "time"';

The difference between single and double quotes is that double quotes allows for string interpolation, meaning that you can reference variables inline in the string and their values will be evaluated in the string like such

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

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

As per your last edit of your question I think the easiest thing you may be able to do that this point is to use a 'heredoc.' They aren't commonly used and honestly I wouldn't normally recommend it but if you want a fast way to get this wall of text in to a single string. The syntax can be found here: http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredocand here is an example:

根据您对问题的最后编辑,我认为您可以做的最简单的事情是使用“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;

回答by Matoeil

Use the addslashesfunction:

使用addlashes函数:

 $str = "Is your name O'Reilly?";

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

回答by Your Common Sense

Save your text not in a PHP file, but in an ordinary text file called, say, "text.txt"

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

Then with one simple $text1 = file_get_contents('text.txt');command have your text with not a single problem.

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

回答by jarod

Use htmlspecialchars(). Then quote and less / greater than symbols don't break your HTML tags~

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

回答by Manse

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

or

或者

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

回答by rogerlsmith

You can use the PHP function addslashes()to any string to make it compatible

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

回答by Manos Dilaverakis

Either escape the quote:

要么逃避引用:

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

or use single quotes to denote your string:

或使用单引号表示您的字符串:

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