开发者

Problems with embedded apex in a string?

开发者 https://www.devze.com 2022-12-29 03:00 出处:网络
how can I write more embedded \" and \' in php ? For example, I dunno how to write this html complete element with all apex:

how can I write more embedded " and ' in php ? For example, I dunno how to write this html complete element with all apex:

As you can see, I use '' for the php string. Then inside, I use "", but then I need another level of apix and I dunno how to write that one in my php document. (php thinks that the string is complete in the middle because it sees another ' before the end.

$output .= '<im开发者_JAVA技巧g style="outline:none;" src="sites/default/unselect.png" alt="Unselect All" onclick='$(this).siblings('.form-item').each(function(index){ $('input:checkbox', this).attr('checked', ''); });'/>';

how can I solve this ?

thanks


there are many ways to solve that. Listed from best to worst

1) Use a templating engine and keep your php and your html separate. Also, use non-obtrusive javascript and avoid javascript code in html tags

2) When outputting complex html, escape from php mode and print strings as is.

function foo() { ?>
       some complex html
<?php 
    php continues here
 } // foo

3) Use heredoc syntax to avoid quoting issues

$output .= <<<TEXT
   <img style="outline:none;" src="sites/default/unselect.png" alt="Unselect All" onclick='$(this).siblings('.form-item').each(function(index){ $('input:checkbox', this).attr('checked', ''); });'/>
TEXT;

4) Escape quotes as others said


$output .= '<img style="outline:none;" src="sites/default/unselect.png" alt="Unselect All" onclick="$(this).siblings(\'.form-item\').each(function(index){ $(\'input:checkbox\', this).attr(\'checked\', \'\'); });"/>';

Your onclick itself also had the wrong quote


please, use the "\" before quote

0

精彩评论

暂无评论...
验证码 换一张
取 消