Javascript php脚本中的双引号echo
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11036420/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Double quotes within php script echo
提问by RXC
I have a line of php code that looks like this:
我有一行 php 代码,如下所示:
echo "<script>$('#edit_errors').html('<h3><em>Please Correct Errors Before Proceeding</em></h3>')</script>";
I would like to know how to add a font color to the text correctly. If I do this:
我想知道如何正确地为文本添加字体颜色。如果我这样做:
echo "<script>$('#edit_errors').html('<h3><em><font color="red">Please Correct Errors Before Proceeding</font></em></h3>')</script>";
The word "red" is in black text and the compiler throws an error.
“红色”一词是黑色文本,编译器会抛出错误。
If I use single quotes around red, then the text does not show up at all.
如果我在红色周围使用单引号,则文本根本不会显示。
Any help would be great. Thanks
任何帮助都会很棒。谢谢
回答by Zbigniew
You need to escape "
, so it won't be interpreted as end of string. Use \
to escape it:
你需要转义"
,所以它不会被解释为字符串的结尾。使用\
逃脱它:
echo "<script>$('#edit_errors').html('<h3><em><font color=\"red\">Please Correct Errors Before Proceeding</font></em></h3>')</script>";
Read more: stringsand escape sequences
回答by Marc B
回答by John Conde
Just escape your quotes:
只是逃避你的报价:
echo "<script>$('#edit_errors').html('<h3><em><font color=\"red\">Please Correct Errors Before Proceeding</font></em></h3>')</script>";
回答by Ryan
You need to escape the quotes in the string by adding a backslash \
before "
.
您需要通过\
在"
.之前添加反斜杠来转义字符串中的引号。
Like:
喜欢:
"<font color=\"red\">"
回答by mark
if you need to access your variables for an echo statement within your quotes put your variable inside curly brackets
如果您需要访问引号内的 echo 语句的变量,请将变量放在大括号内
echo "i need to open my lock with its: {$array['key']}";
回答by mario
You can just forgo the quotes for alphanumeric attributes:
您可以放弃字母数字属性的引号:
echo "<font color=red> XHTML is not a thing anymore. </font>";
echo "<div class=editorial-note> There, I said it. </div>";
Is perfectly valid in HTML, and though still shunned, absolutely en vogue since HTML5.
在 HTML 中完全有效,尽管仍然被回避,但自 HTML5 以来绝对流行。
CAVEATS
警告
- It's only valid for mostly alphanumeric and dash combinations.
- Never ever do this with user input appended to attributes (those need quoting and
htmlspecialchars
or some whitelisting). - See also: When the attribute value can remain unquoted in HTML5
- In other news:
<font>
specifically is somewhat outdated however.
- 它仅对大部分字母数字和破折号组合有效。
- 永远不要将用户输入附加到属性(那些需要引用和/
htmlspecialchars
或某些白名单)来执行此操作。 - 另请参阅:当属性值可以在 HTML5 中保持不带引号时
- 在其他新闻中:
<font>
然而,特别是有些过时。