在PHP中转义引号

我得到parsing错误,我认为这是因为"time"的引号。 我怎样才能把它作为一个整体的string呢?

 <?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%"; 

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

像这样使用反斜杠

 "From time to \"time\""; 

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

 'From time to "time"'; 

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

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

根据你最后一次编辑你的问题,我认为你可以做的最简单的事情就是使用“heredoc”。 他们不常用,老实说,我通常不会推荐它,但如果你想要一个快速的方法来获得这个墙的文字在一个单一的string。 语法可以在这里find: 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; 

使用addslashesfunction:

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

保存你的文本不是在PHP文件中,而是在普通的文本文件中调用,比如说“text.txt”

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

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

要么

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

您可以使用PHP函数addslashes()来使任何string兼容

http://php.net/manual/en/function.addslashes.php

要么逃避报价:

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

或者用单引号来表示你的string:

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