在PHP中转义引号 [英] Escaping quotation marks in PHP

查看:75
本文介绍了在PHP中转义引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了解析错误,我认为这是由于"time"上的引号引起的.我怎样才能将其视为一个完整的字符串?

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?

<?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.

推荐答案

使用反斜杠

"From time to \"time\"";

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

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

'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"

根据您对问题的最后一次修改,我认为您可能能够做到的最简单的事情是使用"heredoc".它们并不常用,老实说,我通常不推荐这样做,但是如果您想要一种快速的方法来将文本墙放入单个字符串中,则不建议使用它们.语法可以在这里找到: http://www.php.net/manual/zh-CN/language.types.string.php#language.types.string.syntax.heredoc ,下面是一个示例:

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.heredoc and here is an example:

$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;

这篇关于在PHP中转义引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆