dou47732 2010-08-17 15:29
浏览 25
已采纳

引用和转义文本字符串建议

I am mainly C++ programmer who returns to PHP seasonly. Every time I return I spend time to get used to echo/print correct JavaScript/HTML/CSS regarding the single quote and double quote ...etc. I need an advice in the form of a set of rules to follow for good escaping practice.

Edit This is a small example for my bad working code that I need to follow a rule to get more readable :

$ret = "<a href=\"$url\"><img src=\"images/delete.png\" width=\"20\" height=\"20\"></img></a>";

Thanks

  • 写回答

2条回答 默认 最新

  • dow72046 2010-08-17 17:26
    关注

    I'd rather use the single quote syntax for working with HTML. Reasons:

    1. it's faster ( noticable with larger strings with a bigger number of iterations )
    2. it's easier to know where the vars are
    3. html attributes need to be escaped using htmlspecialchars()
    4. you don't have to escape every doublequote

    So that string would look like:

    $ret = '<a href="'.htmlspecialchars($url).'"><img src="images/delete.png" width="20" height="20"></img></a>';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制