不让HTML代码在页面上编译

不让HTML代码在页面上编译,第1张

你的文章经过html编码后在进行发表,则<a>百度</a>仍然显示为原样,不会变成超链接:function html_encode(strHTML)   //使用此函数编码你的文章

{   

  var strTem = ""   

  if (strHTML.length == 0) return ""   

  strTem = strHTML.replace(/&/g, "&gt")   

  strTem = strTem.replace(/</g, "&lt")   

  strTem = strTem.replace(/>/g, "&gt")   

  strTem = strTem.replace(/ /g, "&nbsp")   

  strTem = strTem.replace(/\'/g, "&#39")   

  strTem = strTem.replace(/\"/g, "&quot")   

  strTem = strTem.replace(/\n/g, "<br>")   

  return strTem   

}

//编码后在把内容赋值给相应的显示控件即可

//使用下面这句测试一下显示的内容:

document.write(html_encode(document.body.innerHTML))

通过PHP的一个内置的函数htmlspecialchars($html)

如果你要是先把一段HTML代码存入数据库在取出来的话

可以这样入库是 htmlspecialchars($html , ENT_QUOTES)

出库是 htmlspecialchars_decode($html , ENT_QUOTES)

<?php 

    echo '<pre>'

    var_dump(file_get_contents('http://www.google.com.hk'))

    

    //or use this method

    echo htmlentities(file_get_contents('http://www.google.com.hk'))


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/7331894.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-04
下一篇 2023-04-04

发表评论

登录后才能评论

评论列表(0条)

保存