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条)

报告相同问题?

悬赏问题

  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写