如何在PHP中转义引号?

时间:2014-01-14 13:36:02

标签: php jquery echo

我有那段代码:

<a href="#" onclick="$('#two').html('<img src=\'http://willstern.com/web/wp-content/uploads/2011/06/jquerylogo2.png\' />');return false;">Add an Img</a>

我不知道如何将它放在echo ' code ' ;

4 个答案:

答案 0 :(得分:1)

这将有效(已测试)

<?php 

  echo "<a href=\"#\" onclick=\"$('#two').html('<img src=\'http://willstern.com/web/wp-content/uploads/2011/06/jquerylogo2.png\' />');return false;\">Add an Img</a>";

?>

要在php中转义单引号或双引号,请在\' \" see the documentation.之前添加反斜杠

啊是的,jQuery see the documentation.

也是如此

当您使用jQuery时,我建议您将JS与HTML分开一点,您可以编写相同的代码。

<a href="#" class="add_an_image">Add an Image</a>

<script>

  $('.add_an_image').click(function(){

    $('#two').html('<img src="http://willstern.com/web/wp-content/uploads/2011/06/jquerylogo2.png" />');
    return false;

  });

</script>

答案 1 :(得分:0)

试试这个:

$code = "<a href=\"#\" onclick=\"$('#two').html('<img src=\'http://willstern.com/web/wp-content/uploads/2011/06/jquerylogo2.png\' />');return false;\">Add an Img</a>";

echo $code;

答案 2 :(得分:0)

echo "<a href=\"#\" onclick=\"$('#two').html('<img src=\'http://willstern.com/web/wp-content/uploads/2011/06/jquerylogo2.png\' />');return false;\">Add an Img</a>";

答案 3 :(得分:0)

您也可以尝试heredoc语法:

<?php
$str = <<<EOD
<a href="#" onclick="$('#two').html('<img src=\'http://willstern.com/web/wp-content/uploads/2011/06/jquerylogo2.png\' />');return false;">Add an Img</a>
EOD;
echo $str;