草庐IT

php - 在 PHP 中转义引号

coder 2023-06-12 原文

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

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

最佳答案

这样使用反斜杠

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

关于php - 在 PHP 中转义引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7999148/

有关php - 在 PHP 中转义引号的更多相关文章

  1. ruby - 用逗号、双引号和编码解析 csv - 2

    我正在使用ruby​​1.9解析以下带有MacRoman字符的csv文件#encoding:ISO-8859-1#csv_parse.csvName,main-dialogue"Marceu","Giveittohimóhe,hiswife."我做了以下解析。require'csv'input_string=File.read("../csv_parse.rb").force_encoding("ISO-8859-1").encode("UTF-8")#=>"Name,main-dialogue\r\n\"Marceu\",\"Giveittohim\x97he,hiswife.\"\

  2. ruby - 如何使用文字标量样式在 YAML 中转储字符串? - 2

    我有一大串格式化数据(例如JSON),我想使用Psychinruby​​同时保留格式转储到YAML。基本上,我希望JSON使用literalstyle出现在YAML中:---json:|{"page":1,"results":["item","another"],"total_pages":0}但是,当我使用YAML.dump时,它不使用文字样式。我得到这样的东西:---json:!"{\n\"page\":1,\n\"results\":[\n\"item\",\"another\"\n],\n\"total_pages\":0\n}\n"我如何告诉Psych以想要的样式转储标量?解

  3. ruby -\'(反斜杠,单引号)在 Ruby 字符串中 - 2

    我正在使用Ruby1.8.7,试图生成一个带有\'字符的字符串,以便创建一个在MySQL中运行的脚本。结果应该是这样的:INSERTINTOtable(name,description)values('Joanad\'Arc','')但我不能在ruby​​字符串中只得到一个反斜杠。使用以下代码:string="INSERTINTOtable(name,description)values('Joanad\\'Arc','')"我得到了以下字符串:INSERTINTOtable(name,description)values('Joanad\\'Arc','')还有:string="IN

  4. ruby - 如何在ruby中转换为big endian - 2

    我有一个小端顺序的字符串,作为十六进制编码的字符串000000020597ba1f0cd423b2a3abb0259a54ee5f783077a4ad45fb6200000218000000008348d1339e6797e2b15e9a3f2fb7da08768e99f02727e4227e02903e43a42b31511553101a051f3c00000000000000800000000000000000000000000000000000000000000000000000000000000000000000000000000080020000我想将每个32位block从l

  5. ruby-on-rails - 在 fastercsv 中转义逗号值 - 2

    我正在研究csv生成。我正在分隔由逗号(,)分隔的值。如果字段中的值包含逗号,则不应在excel中分隔该字段。所以我想在那里放一个转义字符。我正在使用FasterCsv。那么我如何放置转义字符。fastercsv的转义字符是什么? 最佳答案 只需引用每个字段(默认为双引号),其中的逗号将被忽略:CSV.generate(:col_sep=>',',:quote_char=>'"')do|row|row"\"Quid,quid\",latinumdictum\n\"sit,altum\",viditur.\n"

  6. ruby-on-rails - 第 1 行中的引号缺失或遗漏 (CSV::MalformedCSVError) - 2

    我在ruby​​/rails中导入此CSV文件时遇到问题我得到的错误信息是这样的:Missingorstrayquoteinline1(CSV::MalformedCSVError)但我不确定发生了什么,因为我的CSV看起来非常好。以下是示例数据:"lesley_grades","lesley_id","last","first","active","site","cohort","section","sections_title","faculty","completed_term_cred","term","sec_start_date","sec_end_date","grade

  7. ruby - 艰难地学习 Ruby 第 9 章三重引号 - 2

    ZedShaw的LearnRubytheHardWay第9章使用三重双引号:puts"""There'ssomethinggoingonhere.Withthethreedouble-quotes.We'llbeabletotypeasmuchaswelike.Even4linesifwewant,or5,or6."""我试着用单双引号写同样的东西,它似乎工作正常。我不明白三引号和单双引号之间的区别。我错过了什么吗? 最佳答案 我不知道他为什么在他的书中使用三重双引号。它们没什么特别的,一个双引号就可以了。这是ruby​​的一个鲜为

  8. ruby-on-rails - 这个 C 和 PHP 程序员如何学习 Ruby 和 Rails? - 2

    按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visitthehelpcenter指导。关闭9年前。我来自C、php和bash背景,很容易学习,因为它们都有相同的C结构,我可以将其与我已经知道的联系起来。然后2年前我学了Python并且学得很好,Python对我来说比Ruby更容易学。然后从去年开始,我一直在尝试学习Ruby,然后是Rails,我承认,直到现在我还是学不会,讽刺的是那些打着简单易学的烙印,但是对于我这样一个老练的程序员来说,我只是无法将它

  9. ruby-on-rails - ruby:将数组的每个元素用附加引号引起来 - 2

    我有以下字符串:a="001;Barbara;122"我拆分成字符串数组:names=a.split(";")names=["001","Barbara","122"]我应该怎么做才能将每​​个元素另外用''引号括起来?结果应该是names=["'001'","'Barbara'","'122'"]我知道这听起来很奇怪,但我需要它在ruby​​onrails中进行数据库查询。出于某种原因,如果我的名字在“”引号中,我将无法访问数据库记录。我在数据库中确实有mk1==0006但rails不想以某种方式访问​​它。但是,它确实访问1222。sql="SELECTmk1,mk2,pk1,pk

  10. ruby - ruby 中的反引号与系统 - 2

    根据我在网上的所有阅读,backtick和system之间的区别是返回的内容。backtick返回STDOUT而system返回true或false。我被告知他们都使用subshel​​l来执行操作。但是我注意到另一个不同之处。output=system('aaa')puts"outputis:#{output}"output=`aaa`puts"outputis:#{output}"上面代码的结果是$rubytest.rboutputis:lab.rb:4:in``':Nosuchfileordirectory-aaa(Errno::ENOENT)fromtest.rb:4:in`'

随机推荐