字符串转义为HTML
有时候后台返回的数据中有字符串,并需要将字符串转化为HTML,下面封装了一个方法,如下
// html转义
function htmlspecialchars_decode(string, quote_style) {
var optTemp = 0
, i = 0
, noquotes = false;
if (typeof quote_style === 'undefined') {
quote_style = 2;
}
string = string.toString().replace(/</g, '<').replace(/>/g, '>');
var OPTS = {
'ENT_NOQUOTES': 0,
'ENT_HTML_QUOTE_SINGLE': 1,
'ENT_HTML_QUOTE_DOUBLE': 2,
'ENT_COMPAT': 2,
'ENT_QUOTES': 3,
'ENT_IGNORE': 4
};
if (quote_style === 0) {
noquotes = true;
}
if (typeof quote_style !== 'number') {
quote_style = [].concat(quote_style);
for (i = 0; i < quote_style.length; i++) {
if (OPTS[quote_style[i]] === 0) {
noquotes = true;
} else if (OPTS[quote_style[i]]) {
optTemp = optTemp | OPTS[quote_style[i]];
}
}
quote_style = optTemp;
}
if (quote_style & OPTS.ENT_HTML_QUOTE_SINGLE) {
string = string.replace(/�*39;/g, "'");
}
if (!noquotes) {
string = string.replace(/"/g, '"');
}
string = string.replace(/&/g, '&');
return string;
}
为了便于更好的使用该方法,下面是关于转义为html的案例,如下
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<div class="texts"> </div>
//点击“请求”按钮,开始执行转义并展示
<button class="btns">请求数据</button>
<div class="containerBox"></div>
</body>
<script src="vote/js/jquery_v3.3.1.js"></script>
<script>
$(function(){
$.ajax({
url: '1.json',
type: 'post',
})
.done(function(res) {
console.log(res.data[0]);
var aaa = res.data[0].sas;
var ddd = $('.texts').text(aaa)
htmlspecialchars_decode(res.data[0].sas) })
.fail(function() {
console.log("error");
})
.always(function() {
console.log("complete");
}); }) $('.btns').click(function() {
getReq();
});
function getReq(){
$.ajax({
url: '1.json',
type: 'post',
success:function(res){
console.log(res.data);
$.each(res.data, function(i, values) {
console.log(i)
console.log(values.sas);
var objHtml = htmlspecialchars_decode(values.sas)
$('.containerBox').append(objHtml)
});
},
error:function(e){
console.log(error)
}
}); }
// html转义,调用的时候只需要传第二个值即可
function htmlspecialchars_decode(string, quote_style) {
var optTemp = 0
, i = 0
, noquotes = false;
if (typeof quote_style === 'undefined') {
quote_style = 2;
}
string = string.toString().replace(/</g, '<').replace(/>/g, '>');
var OPTS = {
'ENT_NOQUOTES': 0,
'ENT_HTML_QUOTE_SINGLE': 1,
'ENT_HTML_QUOTE_DOUBLE': 2,
'ENT_COMPAT': 2,
'ENT_QUOTES': 3,
'ENT_IGNORE': 4
};
if (quote_style === 0) {
noquotes = true;
}
if (typeof quote_style !== 'number') {
quote_style = [].concat(quote_style);
for (i = 0; i < quote_style.length; i++) {
if (OPTS[quote_style[i]] === 0) {
noquotes = true;
} else if (OPTS[quote_style[i]]) {
optTemp = optTemp | OPTS[quote_style[i]];
}
}
quote_style = optTemp;
}
if (quote_style & OPTS.ENT_HTML_QUOTE_SINGLE) {
string = string.replace(/�*39;/g, "'");
}
if (!noquotes) {
string = string.replace(/"/g, '"');
}
string = string.replace(/&/g, '&');
return string;
}
</script>
</html>
里面的json数据为
{
"data":[{
"sas":"<section data-id="23210" class="xmyeditor" data-type="lspecial06" data-tools="" powered-by="xmyeditor.com"> <section style="text-align: center;" powered-by="xmyeditor.com"> <section style="color: #fff;line-height: 80px;text-shadow: 0 1px #ccc, 1px 0 #ccc, -1px 0 #ccc, 0 -1px #ccc;" powered-by="xmyeditor.com"> <p style="padding: 0px;margin: 0px;font-size:80px;"> <strong class="autosort">1</strong> </p> </section> </section> <section style="text-align: center;margin-top: -45px;" powered-by="xmyeditor.com"> <section style="background: url(http://alstyle.xmyeditor.com/sucai-png/20190124/5c498d915285ffwwwdwjjcz.qxwrkofsmliqxrpznudguodmwtvlmj);background-repeat: no-repeat;background-size: 100%;width: 100px;height: 30px;display: inline-block;" powered-by="xmyeditor.com"> <section style="margin-top: 5px;" powered-by="xmyeditor.com"> <section style="color: #333;letter-spacing: 1px;line-height: 24px;" powered-by="xmyeditor.com"> <p style="padding: 0px;margin: 0px;font-size: 15px;"> <strong class="xmybrush">五月</strong> </p> </section> </section> </section> <section style="color: #666;letter-spacing: 1px;line-height: 24px;" powered-by="xmyeditor.com"> <p style="padding: 0px;margin: 0px;font-size: 17px;" class="xmybrush"> 劳动节 </p> </section> </section> </section>"
},{
"sas":"<section data-id="23210" class="xmyeditor" data-type="lspecial06" data-tools="" powered-by="xmyeditor.com"> <section style="text-align: center;" powered-by="xmyeditor.com"> <section style="color: #fff;line-height: 80px;text-shadow: 0 1px #ccc, 1px 0 #ccc, -1px 0 #ccc, 0 -1px #ccc;" powered-by="xmyeditor.com"> <p style="padding: 0px;margin: 0px;font-size:80px;"> <strong class="autosort">1</strong> </p> </section> </section> <section style="text-align: center;margin-top: -45px;" powered-by="xmyeditor.com"> <section style="background: url(http://alstyle.xmyeditor.com/sucai-png/20190124/5c498d915285ffwwwdwjjcz.qxwrkofsmliqxrpznudguodmwtvlmj);background-repeat: no-repeat;background-size: 100%;width: 100px;height: 30px;display: inline-block;" powered-by="xmyeditor.com"> <section style="margin-top: 5px;" powered-by="xmyeditor.com"> <section style="color: #333;letter-spacing: 1px;line-height: 24px;" powered-by="xmyeditor.com"> <p style="padding: 0px;margin: 0px;font-size: 15px;"> <strong class="xmybrush">六月</strong> </p> </section> </section> </section> <section style="color: #666;letter-spacing: 1px;line-height: 24px;" powered-by="xmyeditor.com"> <p style="padding: 0px;margin: 0px;font-size: 17px;" class="xmybrush"> 儿童节 </p> </section> </section> </section>"
},{
"sas":"<section data-id="23210" class="xmyeditor" data-type="lspecial06" data-tools="" powered-by="xmyeditor.com"> <section style="text-align: center;" powered-by="xmyeditor.com"> <section style="color: #fff;line-height: 80px;text-shadow: 0 1px #ccc, 1px 0 #ccc, -1px 0 #ccc, 0 -1px #ccc;" powered-by="xmyeditor.com"> <p style="padding: 0px;margin: 0px;font-size:80px;"> <strong class="autosort">1</strong> </p> </section> </section> <section style="text-align: center;margin-top: -45px;" powered-by="xmyeditor.com"> <section style="background: url(http://alstyle.xmyeditor.com/sucai-png/20190124/5c498d915285ffwwwdwjjcz.qxwrkofsmliqxrpznudguodmwtvlmj);background-repeat: no-repeat;background-size: 100%;width: 100px;height: 30px;display: inline-block;" powered-by="xmyeditor.com"> <section style="margin-top: 5px;" powered-by="xmyeditor.com"> <section style="color: #333;letter-spacing: 1px;line-height: 24px;" powered-by="xmyeditor.com"> <p style="padding: 0px;margin: 0px;font-size: 15px;"> <strong class="xmybrush">七月</strong> </p> </section> </section> </section> <section style="color: #666;letter-spacing: 1px;line-height: 24px;" powered-by="xmyeditor.com"> <p style="padding: 0px;margin: 0px;font-size: 17px;" class="xmybrush"> 建军节 </p> </section> </section> </section>"
}
]
}
直接运行即可显示转义为HTML的布局
字符串转义为HTML的更多相关文章
- 是否采用Sybase形式的自动字符串转义(用 '' 表示 ')
;; 关于php.ini ;; ; 这个文件必须命名为'php.ini'并放置在httpd.conf中PHPINIDir指令指定的目录中. ; 最新版本的php.ini可以在下面两个位置查看: ; h ...
- python字符串转义与正则表达式特殊字符转义
最近在自学python,字符串和正则表达式的特殊字符转义有点混淆,做个笔记简单总结一下. 1.普通字符串转义 在字符串中使用特殊字符时,要用反斜杠(\)转义字符.例如:'Let\'s go!',这里对 ...
- PHP字符串转义
与PHP字符串转义相关的配置和函数如下: 1.magic_quotes_runtime 2.magic_quotes_gpc 3.addslashes()和stripslashes() 4.mysql ...
- 从 RegExp 构造器看 JS 字符串转义设计
多年前我第一次入职腾讯的时候,DC 从杭州给我寄来了一本他刚翻译出炉的<高性能 JavaScript>.那段时间为了帮忙校对,我仔细阅读了书中的每一个段落,结果积累了不少 JavaScri ...
- java字符串转义,把<>转换成<>等字符【原】
java字符串转义,把<>转换成<>等字符 使用的是commons-lang3-3.4 中的StringEscapeUtils类 package test; import ja ...
- react将字符串转义成html语句
在使用reactjs库的时候,会遇到将一段html的字符串,然后要将它插入页面中以html的形式展现,然而直接插入的话页面显示的就是这段字符串,而不会进行转义,可以用以下方法插入,便可以html的形式 ...
- mybatis字符串转义问题
问题描述 @Select("select * from account order by #{orderBy} #{orderRule} limit #{start},#{offset}&q ...
- PHP实现字符串转义和还原
首先大家可以简单了解下什么是转义字符?有什么用? 转义字符是一种特殊的字符常量.转义字符以反斜线"\"开头,后跟一个或几个字符.转义字符具有特定的含义,不同于字符原有的意义,故称“ ...
- PHP怎么实现字符串转义和还原?
首先大家可以简单了解下什么是转义字符?有什么用? 转义字符是一种特殊的字符常量.转义字符以反斜线"\"开头,后跟一个或几个字符.转义字符具有特定的含义,不同于字符原有的意义,故称“ ...
随机推荐
- selenium定位方式-Xpath使用方法
什么是Xpath? XPath是XML的路径语言,通俗一点讲就是通过元素的路径来查找到这个标签元素. 一. 在火狐浏览器上安装Xpath 方法如下: 1.使用 Firefox 访问 https://a ...
- 15行python代码,帮你理解令牌桶算法
本文转载自: http://www.tuicool.com/articles/aEBNRnU 在网络中传输数据时,为了防止网络拥塞,需限制流出网络的流量,使流量以比较均匀的速度向外发送,令牌桶算法 ...
- 标准库类型string
定义和初始化string对象 初始化string对象方式: string s1;//默认初始化,s1是一个字符串 string s2(s1);//s2是s1的副本 string s2 = s1;//等 ...
- [转] C/C++ 调用Python
from : https://cyendra.github.io/2018/07/10/pythoncpp/ 目录 前言 官方文档 环境搭建 编译链接 Demo 解释器 初始化 GIL Object ...
- [Kubernetes]关于 Kubernetes ,你想要的,都在这儿了
陆陆续续,关于 Kubernetes 写了有 20+ 篇文章了. 今天这篇文章来一个整合,从实践到理论,可以按需查看(我是按照博客发表时间来排序的,如果后续有想要更新的内容,也会及时更新到这篇文章中) ...
- [Kubernetes] CRI 的设计与工作原理
咱们来看看,有了 CRI 之后, Kubernetes 的架构图: 我们可以看到, CRI 机制能够发挥作用的核心,在于每一个容器项目现在都可以自己实现一个 CRI shim ,自行对 CRI 请求进 ...
- WPF常见主界面的布局
一.概述 效果图: 暂时没做完,请等待 二.实现 (一)实现无边框窗口 原文:WPF 窗口去除顶部边框(正宗无边框) ============================ 最近在做一个大屏展示视频 ...
- Android OS的image文件组成
Android OS由以下image文件组成: 1)Bootloader ---在设备启动时开始加载Boot image 2)Boot image ---Kernel 和 RAMdisk 3)Syst ...
- 初学python之路-day07-字符编码
今天的博客主要关于字符编码,并对前几天学习的数据类型做些总结. 学习字符编码的目的:解决乱码问题. 应用程序打开文本文件的三步骤 1.打开应用程序 2.将数据加载到内存中 3.cpu将内存中的数 ...
- cc.Lable组件,RichText组件,AudioSouce组件的使用
一.cc.Lable组件的使用 1.创建Label的方法 a.通过菜单直接创建Label组件:b.先创建节点,然后在节点上绑定Label组件即可. 2.Label 面板上的属性 String => ...