我收到Parse错误,我认为这是因为"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%";
问题是我无法在每个引号前手动添加\
。这是我需要比较的实际文本。
答案 0 :(得分:70)
使用反斜杠
"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;
答案 1 :(得分:44)
使用addslashes功能:
$str = "Is your name O'reilly?";
// Outputs: Is your name O\'reilly?
echo addslashes($str);
答案 2 :(得分:4)
将文本保存在php文件中,但保存在名为“text.txt”的普通文本文件中
然后用一个简单的$text1 = file_get_contents('text.txt');
命令让你的文字没有一个问题。
答案 3 :(得分:2)
$text1= "From time to \"time\"";
或
$text1= 'From time to "time"';
答案 4 :(得分:1)
要么逃避引用:
$text1= "From time to \"time\"";
或使用单引号表示您的字符串:
$text1= 'From time to "time"';
答案 5 :(得分:1)
您可以将php函数addslashes()用于任何字符串以使其兼容
答案 6 :(得分:1)
使用htmlspecialchars()。然后引号和小于/大于符号不会破坏您的HTML标记〜