用引号写字符串的替代方法

时间:2012-06-04 22:51:02

标签: php string

我如何在PHP中完成这项工作:

$string = "<div id="widget14" class="widget widget-124">
     <a href="http://website.com/page.php?id={$pageurl}"><p><span class="hotspot" onmouseover="tooltip.show('<strong>About Us</strong><br/>Learn about us!');" onmouseout="tooltip.hide();"><img class="images_button" alt="About Image" src="{$image_url}" width="172px"/><br/>About Us</span></p></a>
    </div>";

2 个答案:

答案 0 :(得分:4)

不需要PHP:

<div id="widget14" class="widget widget-124">
   <a href="http://website.com/cgames/"><p><span class="hotspot" onmouseover="tooltip.show('<strong>About Us</strong><br/>Learn about us!');" onmouseout="tooltip.hide();"><img class="images_button" alt="About Image" src="http://www.website.com/images/logo.png" width="172px"/><br/>About Us</span></p></a>
</div>

如果绝对必须是变量:

$string = <<<HTML
    <div id="widget14" class="widget widget-124">
       <a href="http://website.com/cgames/"><p><span class="hotspot" onmouseover="tooltip.show('<strong>About Us</strong><br/>Learn about us!');" onmouseout="tooltip.hide();"><img class="images_button" alt="About Image" src="http://www.website.com/images/logo.png" width="172px"/><br/>About Us</span></p></a>
    </div>
HTML;

Heredoc Syntax

否则你会用反斜杠来逃避引号。

答案 1 :(得分:0)

$string = "<div id=\"widget14\" class=\"widget widget-124\">
     <a href=\"http://website.com/cgames/\"><p><span class=\"hotspot\" onmouseover=\"tooltip.show('<strong>About Us</strong><br/>Learn about us!');\" onmouseout=\"tooltip.hide();\"><img class=\"images_button\" alt=\"About Image\" src=\"http://www.website.com/images/logo.png\" width=\"172px\"/><br/>About Us</span></p></a>
    </div>\";

上面应该工作得非常好。