在 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.";
发生此错误是因为引号中的引号该字符串使解释器感到困惑。要解决此问题,您可以使用反斜杠 () 转义引号。通过这样做,PHP 会将包含的文本识别为单个字符串,而不解释引号。
$text1 = 'From time to \"time\" this submerged or latent theater in 'Hamlet' ...
或者,您可以对字符串使用单引号,因为 PHP 不区分单引号和双引号对于字符串文字:
$text1 = 'From time to "time"';
要考虑的另一个选项是使用定界符,它是一种使用“
$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;
通过实现这些技术,您可以避免由引号引起的解析错误,并确保您的字符串在您的 PHP 脚本中正确解释。
以上是如何转义 PHP 字符串中的引号以避免解析错误?的详细内容。更多信息请关注PHP中文网其他相关文章!