duanfen1312 2010-04-29 12:47
浏览 26
已采纳

嵌入式顶点在字符串中的问题?

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 .= '<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', ''); });'/>';

how can I solve this ?

thanks

  • 写回答

3条回答 默认 最新

  • dongqiu9018 2010-04-29 13:14
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 luckysheet
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误