简体   繁体   English

我如何在 PHP 和 Javascript 的代码中使用 ' 和 "?

[英]how can i use ' and " in a code in PHP and Javascript?

I'd like how i can use a lot of ' and " in a code.我想如何在代码中使用大量的 ' 和 "。

Example:例子:

echo 'document.write("<a href='$url'> <img src='{$row["image"]}' border='0' /> </a>");';

I tried but i'm getting error.我试过了,但出现错误。 Anyone can help?任何人都可以帮忙吗?

so, you have multi-level problem here:所以,你在这里有多层次的问题:

  1. data which is echoed to html, usually should be properly escaped via htmlspecialchars回显到 html 的数据,通常应该通过 htmlspecialchars 正确转义
  2. you want to see document.write("..."..."); in your finally produced html, this will trigger javascript syntax error你想看到document.write("..."...");在你最终生成的 html 中,这将触发 javascript 语法错误
  3. to avoid this error, you should use \\ before " inside string为避免此错误,您应该在 " 内部字符串之前使用\\

echo 'document.write("<a href=\\"' . htmlspecialchars($url) . '\\"><img src=\\"' . htmlspecialchars($row["image"]) . '\\" border=\\"0\\" /></a>");';

note: I'm using echo with single quotes, if you're using double quotes - you will have to double \\\\注意:我使用的是带单引号的 echo,如果您使用双引号 - 您必须加倍\\\\

in case of double quotes your code will look like:在双引号的情况下,您的代码将如下所示:

echo "document.write(\\"<a href=\\\\\\"" . htmlspecialchars($url) . "\\\\\\"><img src=\\\\\\"" . htmlspecialchars($row["image"]) . "\\\\\\" border=\\\\\\"0\\\\\\" /></a>\\");";

Here are three ways to tackle this problem.以下是解决这个问题的三种方法。

1. Escaping the inner double slashes 1.转义内部双斜线

echo "document.write('<a href=\\"$url\\"> <img src=\\"{$row['image']}\\" border=\\"0\\" /> </a>');";

2. Closing your PHP tags and writing javascript 2. 关闭你的 PHP 标签并编写 javascript

?> document.write('<a href="<?php echo $url; ?>"> <img src="<?php echo $row['image']; ?>" border="0" /> </a>'); <?php

3. Using Heredoc syntax 3. 使用Heredoc语法

echo <<<EOJS document.write('<a href="$url"> <img src="{$row['image']}" border="0" /> </a>'); EOJS;

This will work:这将起作用:

<?php
$url = "http://www.google.com";
$row = array("image" => "image.png");
echo "document.write('<a href=\"$url\"> <img src=\"".$row["image"]."\" border=0 /> </a>');";
// output: document.write('<a href="http://www.google.com"> <img src="image.png" border=0 /> </a>');
?>

You can use the heredoc syntax:您可以使用 heredoc 语法:

echo <<<EOT
document.write(<a href='{$url}'> <img src='{$row["image"]}' border='0' /> </a>);

EOT;

From phpdocs来自phpdocs

Heredoc text behaves just like a double-quoted string, without the double quotes. Heredoc 文本的行为就像一个双引号字符串,没有双引号。 This means that quotes in a heredoc do not need to be escaped, but the escape codes listed above can still be used.这意味着heredoc中的引号不需要转义,但仍然可以使用上面列出的转义码。 Variables are expanded, but the same care must be taken when expressing complex variables inside a heredoc as with strings.变量被扩展,但在 heredoc 中表达复杂变量时必须像使用字符串一样小心。

Also note, that还要注意的是

It is very important to note that the line with the closing identifier must contain no other characters, except a semicolon (;).请务必注意,带有结束标识符的行不得包含除分号 (;) 之外的其他字符。 That means especially that the identifier may not be indented, and there may not be any spaces or tabs before or after the semicolon.这尤其意味着标识符不能缩进,并且分号前后不能有任何空格或制表符。 It's also important to realize that the first character before the closing identifier must be a newline as defined by the local operating system.认识到结束标识符之前的第一个字符必须是本地操作系统定义的换行符也很重要。 This is \\n on UNIX systems, including Mac OS X. The closing delimiter must also be followed by a newline.这是 UNIX 系统上的 \\n,包括 Mac OS X。结束定界符后面还必须跟一个换行符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM