function dotran($str) {
$str = str_replace('"','//"',$str);
$str = str_replace("/r/n",'//r//n',$str);
$str = str_replace("/t",'//t',$str);
$str = str_replace("//",'//',$str);
$str = str_replace("/b",'//b',$str);
return $str;
}
这样返回的数据就可以正常显示,下面是转换后的内容:
jsontext='{"jqry":[{"id":"121","userid":"0","status":"1","filename":"","url":"333333333","title":"aaaaaaa","type":"WatchTV","seq":"1","remark":"remarktext","content":"<p>//r//n//t<object classid=//"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000//" codebase=//"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0//"><param name=//"quality//" value=//"high//" /><param name=//"movie//" value=//"http://www.xxx.com/vod/COD4_Ep1.swf//" /><embed pluginspage=//"http://www.macromedia.com/go/getflashplayer//" quality=//"high//" src=//"http://www.xxx.com/vod/COD4_Ep1.swf//" type=//"application/x-shockwave-flash//"></embed></object></p>//r//n"}]}';
HTML转换函数
public string ChangeString(string str)
{
//str含有HTML标签的文本
str = str.Replace("<","<");
str = str.Replace(">",">");
str = str.Replace(" "," ");
str = str.Replace("\n","<br>");
str = str.Replace("&","&");
return str;
}
=======================
js字符过滤html标签互转函数
function htmlencode(str) {
str = str.replace(/&/g, '&');
str = str.replace(/</g, '<');
str = str.replace(/>/g, '>');
str = str.replace(/(?:t| |v|r)*n/g, '<br />');
str = str.replace(/ /g, ' ');
str = str.replace(/t/g, ' ');
str = str.replace(/x22/g, '"');
str = str.replace(/x27/g, ''');
return str;
}
function htmldecode(str) {
str = str.replace(/&/gi, '&');
str = str.replace(/ /gi, ' ');
str = str.replace(/"/gi, '"');
str = str.replace(/'/g, "'");
str = str.replace(/</gi, '<');
str = str.replace(/>/gi, '>');
str = str.replace(/<br[^>]*>(?:(rn)|r|n)?/gi, 'n');
return str;
}
function textencode(str) {
str = str.replace(/&/gi, '&');
str = str.replace(/</g, '<');
str = str.replace(/>/g, '>');
return str;
}
function textdecode(str) {
str = str.replace(/&/gi, '&');
str = str.replace(/</gi, '<');
str = str.replace(/>/gi, '>');
return str;
}
实例1
RT:
返回的Json数据如下、带Html标签就无法显示了、
{"articleList": [{"articleId":6,"title":"拜拜拜拜 吧","author":"superman","content":"BBBBBBBBBBBB","hits":"0"}, {"articleId":7,"title":"333","author":"superman","content":"333333333333333333333333333333333333","hits":"0"}, {"articleId":1,"title":"阿萨 德","author":"heshan","content":"test","hits":"0"}, {"articleId":2,"title":"阿萨德","author":"superman","content":"123<img src="http://localhost:8080/jpkc/admin/kindeditor/plugins/emoticons/0.gif" alt="" border="0" /><br />","hits":"0"},{"articleId":3,"title":"TEST1","author":"superman","content":"TEST1<br />","hits":"0"},{"articleId":4,"title":"测 试","author":"superman","content":"测试功能!!!!","hits":"0"}, {"articleId":5,"title":"test!","author":"superman","content":"KE.util.setFullHtml('content', '');","hits":"0"},]}
解决办法:
"content":"123<img src="http://localhost:8080/jpkc/admin/kindeditor/plugins/emoticons/0.gif" alt="" border="0"/><br/>"
改为
"content":"123<img src='http://localhost:8080/jpkc/admin/kindeditor/plugins/emoticons/0.gif' alt='' border='0' /><br/>"
js方法:str.replaceAll("\"","'");
实例2:
从服务器端以JSON格式将数据传递到客户端后,通过JS显示在HTML页面时,有一些特殊字符不能直接显示,如后台传递过来的是 '<b>msg</b> #' 通过JS显示在HTML页面中时,显示成了 msg # ,并不是msg #,这是由于<与>之间的内容看作是HTML标签了,而以&开头的 与#为HTML实体,所以显示不正常。
解决办法很简单,在JS将其渲染到HTML页面前转换一下即可:
01 | <script type= "text/javascript" > |
02 | var str = '<b>msg</b> #' ; |
03 | document.all.div1.innerHTML= '<pre>' +str+ '</pre>' ; |
06 | String.prototype.displayHtml= function (){ |
08 | var strArr = this .split( '' ); |
09 | //HTML页面特殊字符显示,空格本质不是,但多个空格时浏览器默认只显示一个,所以替换 |
11 | for ( var i = 0; i< str.length;i++){ |
13 | if (htmlChar.indexOf(str.charAt(i)) !=-1){ |
14 | //如果存在,则将它们转换成对应的HTML实体 |
15 | switch (str.charAt(i)) { |
17 | strArr.splice(i,1, '<' ); |
20 | strArr.splice(i,1, '>' ); |
23 | strArr.splice(i,1, '&' ); |
27 | return strArr.join( '' ); |
29 | alert(str.displayHtml()); |
30 | document.all.div2.innerHTML=str.displayHtml(); |