为什么要对字符串编码?


某些字符串中包含html标签,不编码,页面输出就乱了。

PHP下怎么对字符串编码?


htmlentities
vs htmlspecialchars

htmlentities 与htmlspecialchar 区别:

htmlentities is
identical to htmlspecialchars() in
all ways, except with htmlentities(),
all characters which have HTML character entity equivalents are translated into these entities.

除了不加参数直接调用htmlentities()以外,这2个function是等价的。

不加参数直接调用htmlentities()时,会把输入的字符串全部当作html标签去编码,所以会产生乱码。

htmlspecialchars只处理这几种字符:

  • '&' (ampersand) becomes '&'
  • '"' (double quote) becomes '"' when ENT_NOQUOTES is
    not set.
  • "'" (single quote) becomes ''' (or ') only when ENT_QUOTES is
    set.
  • '<' (less than) becomes '&lt;'
  • '>' (greater than) becomes '&gt;'

都支持同样的编码参数:

ENT_COMPAT Will convert double-quotes and leave single-quotes alone.
ENT_QUOTES Will convert both double and single quotes.
ENT_NOQUOTES Will leave both double and single quotes unconverted.
ENT_IGNORE Silently discard invalid code unit sequences instead of returning an empty string. Using this flag is discouraged as it » may
have security implications
.
ENT_SUBSTITUTE Replace invalid code unit sequences with a Unicode Replacement Character U+FFFD (UTF-8) or &#FFFD; (otherwise) instead of returning an empty string.
ENT_DISALLOWED Replace invalid code points for the given document type with a Unicode Replacement Character U+FFFD (UTF-8) or &#FFFD; (otherwise) instead of leaving them as is. This may be useful, for instance, to ensure
the well-formedness of XML documents with embedded external content.
ENT_HTML401 Handle code as HTML 4.01.
ENT_XML1 Handle code as XML 1.
ENT_XHTML Handle code as XHTML.
ENT_HTML5 Handle code as HTML 5.

参考:点击打开链接http://php.net/manual/zh/function.htmlentities.php

点击打开链接http://php.net/manual/zh/function.htmlspecialchars.php

javascript 中怎么对字符串编码?

js中只有 encodeURI()encodeURIComponent()escape()

前两个只对uri编码,后一个编码范围太大

参考: 点击打开链接http://www.w3school.com.cn/jsref/jsref_encodeURIComponent.asp

点击打开链接http://www.w3school.com.cn/jsref/jsref_encodeURIComponent.asp

为什么要对URI编码?

参考:http://www.cnblogs.com/leaven/archive/2012/07/12/2588746.html点击打开链接

js中如何对包含html标签字符串编码,一般做法:

function html_encode(str) {
var s = "";
if (str.length == 0) return "";
s = str.replace(/&/g, ">");
s = s.replace(/</g, "<");
s = s.replace(/>/g, ">");
s = s.replace(/ /g, " ");
s = s.replace(/\'/g, "'");
s = s.replace(/\"/g, """);
s = s.replace(/\n/g, "<br>");
return s;
}

PHP & Javascript 如何对字符串中包含html标签进行编码 整理的更多相关文章

  1. 【HTML】处理<br>换行符追加到前端换行无效的问题 --- html中渲染的字符串中包含HTML标签无效的处理方法,字符串中包含HTML标签被转义的问题 解决

    需求如下图: 追加给前台后,效果如下: 可以在源码看到: 是将后台给出来的数据,直接当作字符串给填充在了前台HTML中. 而查看浏览器编译后的HTML源码可以发现: 原来字符串中的<br> ...

  2. jsp 页面 摘要, 要截取字符串 ,当时 字符串中包含 html标签,截取后无法显示

    如题: 处理办法: 1.  使用struts标签 <s:property  value ="#text.replaceAll('<[^>]+>','').substr ...

  3. JavaScript确定一个字符串是否包含在另一个字符串中的四种方法

    一.indexOf() 1.定义 indexOf()方法返回String对象第一次出现指定字符串的索引,若未找到指定值,返回-1.(数组同一个概念) 2.语法 str.indexOf(searchVa ...

  4. C# String.Format格式化json字符串中包含"{" "}"报错问题

    json.Append(String.Format("{\"total\":{0},\"row\":{1}}", lineCount, st ...

  5. oracle 使用正则表达式获取字符串中包含的数字

    select REGEXP_REPLACE('字符串中包含的数字123,提取后就是123', '[^0-9]', '') from dual;

  6. Java:判断字符串中包含某字符的个数

    Java:判断字符串中包含某字符的个数 JAVA中查询一个词在内容中出现的次数: public int getCount(String str,String key){ if(str == null ...

  7. Oracle字符串中包含数字、特殊符号的排序

    问题描述: 某小区,需要按照小区.楼栋.单元号.房间号进行排序,但是按照地址描述排序时,因为字符串中包含数字,所以造成了如下的结果, 1号楼之后应该是2号楼,但是查询结果却是10号楼 . 尝试解决 使 ...

  8. php读取出字符串中的img标签中的图片路径

    php读取出字符串中的img标签中的图片路径 $pageContents = '字符串,带img标签'; $pageContents = str_replace('\"','"', ...

  9. js去除字符串中所有html标签及&nbsp符号

    近日在做项目的时候,经常会在页面上处理一些数据.结果发现自己js掌握的并不是很好.那就在这里记录js的点点滴滴吧. 1. 去除字符串中的 html 标签 function delHtmlTag(str ...

随机推荐

  1. 经典 Linux & VIM 教程

    简明 Vim 练级攻略: http://coolshell.cn/articles/5426.html VIM快捷键: http://coolshell.cn/wp-content/uploads/2 ...

  2. NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)"

    原文: http://stackoverflow.com/questions/19874935/afnetworking-2-0-post-issue-cocoa-error-3840json-tex ...

  3. Camtasia Studio8使用教程

    tip:善于使用ctrl+f搜索目录查看内容 目录 软件的下载与安装 软件的汉化与界面介绍 录制前准备与试机录制 录像机的设置 屏幕画 导入与编辑视频(包括图片,音乐,媒体) 时间线轨道时间轴 标记的 ...

  4. 【三支火把】---C语言面试问题总结

    看了一份关于HR在面试一名C程序员可能提问的问题手册,学到了很多,很多都是一些琐碎的知识点,总是你写过很多大型的C程序,但是我敢说,里面也有你没掌握的东西. 1.全局变量和局部变量有何区别? 答:全局 ...

  5. 微软的.NET示例代码放在Github上了

    博客搬到了fresky.github.io - Dawei XU,请各位看官挪步.最新的一篇是:微软的.NET示例代码放在Github上了.

  6. Windows7 64位机上Emgu CV2.4.2安装与配置

    Windows7 64位机上Emgu CV2.4.2安装与配置         分类:             Emgu CV              2012-11-28 17:22     92 ...

  7. Dragons

    http://codeforces.com/problemset/problem/230/A Dragons time limit per test 2 seconds memory limit pe ...

  8. 快速安装VIM开发环境

    *Mac上当前用户的.vim目录打包*:附件地址:http://pan.baidu.com/s/1sj5FjZJ 1. 备份好系统的原来的vim配置文件,以备恢复使用:         mv ~/.v ...

  9. 转:FORM:客制化Form的菜单栏和右鍵菜單

    Oracle EBS还允许客制化Form的菜单栏. 用户最多可以定义45个form-level的trigger,名称必须为SPECIALn, 其中SPECIAL1 to SPECIAL15属于Tool ...

  10. 【转】【2015MIIC】迅雷CTO陈磊:互联网思维会害死很多传统企业

    MIIC2015大会的“跨界与重构”论坛上,迅雷CTO.网心科技CEO陈磊的演讲引起众多共鸣——独家揭秘“互联网大忽悠”,给这群人画了像,互联网大忽悠通常有五招: 第1招,画大饼,给你一个宏伟的目标: ...