我有一个PHP字符串,其中包含单引号和双引号,但我很难将其正确转义。甚至尝试在线引用网站,但他们的结果也出错。
$confirmation .= '<a title="Share on Facebook" target="_blank" href="javascript: void(0)" onclick="window.open('http://www.facebook.com/sharer.php?u=http%3A%2F%2Fmydomain.com%2Fquiz%2F','sharer','toolbar=0,status=0,width=548,height=325');" class="">Share on Facebook</a>';
我不认为双引号需要转义。尽管如此,我的所有尝试都会在加载页面时产生HTTP 500。
此字符串中的单引号如何转义?
答案 0 :(得分:7)
要在PHP中转义嵌套引号,请使用\
$confirmation .= '<a title="Share on Facebook" target="_blank" href="javascript: void(0)" onclick="window.open(\'http://www.facebook.com/sharer.php?u=http%3A%2F%2Fmydomain.com%2Fquiz%2F\',\'sharer\',\'toolbar=0,status=0,width=548,height=325\');" class="">Share on Facebook</a>';
对于包含大量引号的复杂案例,使用heredoc可能更具可读性和实用性:
$confirmation .= <<<EOT
<a title="Share on Facebook" target="_blank" href="javascript:void(0)" onclick="window.open('http://www.facebook.com/sharer.php?u=http%3A%2F%2Fmydomain.com%2Fquiz%2F','sharer','toolbar=0,status=0,width=548,height=325');" class="">Share on Facebook</a>
EOT;
答案 1 :(得分:0)
$confirmation .= '<a title="Share on Facebook" target="_blank" href="javascript: void(0)" onclick="window.open(\'http://www.facebook.com/sharer.php?u=http%3A%2F%2Fmydomain.com%2Fquiz%2F\',\'sharer\',\'toolbar=0,status=0,width=548,height=325\');" class="">Share on Facebook</a>';