需要处理字符 ,< 到 > 等的转换
解决方案 »
- PHP页面留言板添加每个IP限制回复一次
- PHP环境安全配置教程
- 新手求助!查询语句
- php读取Sqlserver的问题,求教
- 连接数据库类问题
- 请教FLEA框架下,如何只查询主表的一个字段
- 字符串写入图片后不能正常换行,请大家帮帮俺
- php读取CSV乱码
- 请教:做PHP生成静态页面的时候,生成页面的点击率和热门文章排行怎么做
- 有关php的安装,急!急!急!!!!!!!
- 急:调用 webservice 出现 错误:Message = SoapFault exception: [HTTP] Error Fetching http headers
- apche RewriteRule 如何将https转成http?
或者显示的时候做html编码.
//区域框特殊标记替换
function strHtml($str)
{
if(empty($str)) return false;
$str=str_replace('<','<',$str);
$str=str_replace('>','>',$str);
$str=str_replace("\n",'<br>',$str);
$str=str_replace(" ",' ',$str);
return $str;
}//区域框特殊标记还原
function htmlStr($str)
{
if(empty($str)) return false;
$str=str_replace('<','<',$str);
$str=str_replace('>','>',$str);
$str=str_replace("<br>","\n",$str);
$str=str_replace(" ",' ',$str);
return $str;
}
strp_tags():去除<和>标签及当中的内容
addslashes():给五个字符转义,防止mysql误读
htmlspecialchars():不知道建议你看编辑器源码:看其对文本框的处理过程。如没有处理,可按以下常规处理:
第一种方法:PHP端处理:判断应用addslashes(),这只是入库,出库用stripslashes()
注意:入库和出库的判断设置变量不一样第二中:如果有源码显示
可以将用js或PHP端进行替换,将<和>转换成特别代码,显示时回转:< 到 > 转换就是这个意思
$fck_value = htmlspecialchars(stripcslashes($_POST['fck']));
} else {
$fck_value = htmlspecialchars($_POST['fck']);
}从数据库取值的时候htmlspecialchars_decode($mysql_fck_vaule);
通常我用的是ajax的提交,用jquery 库来实现的.所以在提交时会将发送的表单转成json这种格式的进行发送.
例如我对新闻的基础数据的一次抽取. var oEditor = FCKeditorAPI.GetInstance('<?=$tableColumns[2];?>') ;
data = {
news_id:$('#news_id').val(),
timeStamp:new Date().getTime(),
news_title:$('#news_title').val(),
news_category:$('#news_category').val(),
news_src:$('#news_src').val(),
news_content :oEditor.GetXHTML( true ).toString().Trim()
};
在接收数据要在将其值赋到fck 时. $fckValue = $$tableColumns[2];
$fckValue = preg_replace ("/\n/i","",$fckValue); //碰到什么特别的麻烦就在这里将其替换掉.
$fckValue = addslashes ( $fckValue );
如果只是在页面显示,直接读库输出就OK了,暂时还没有碰到什么实质性的问题.关于js格式转换,我最近整理了几个常用的函数.通常至少要trim一遍.// JavaScript Document
String.prototype.Trim=function(){return this.replace(/(^\s*)|(\s*$)/g,"");} String.prototype.cleanStyle=function(){
var html =this ;
//清除 H\d 标记
html = html.replace( /<H\d>(.*)<\/H\d>/gi, '$1' ) ;
//html = html.replace( /<H\d>\s*<\/H\d>/gi, '' ) ;
//去掉其中的一个样式属性。 修如.
//remove Styles
html = html.replace(/<(\w[^>]*) style="([^\"]*)"([^>]*)/gi, "<$1$3" ) ;
// remove Class
html = html.replace(/<(\w[^>]*) class=([^ |>]*)([^>]*)/gi, "<$1$3") ;
html = html.replace(/<\!--.*-->/ig, "" ) ;
//去除多余的span 标签.
html = html.replace( /<SPAN\s*[^>]*>\s* \s*<\/SPAN>/gi, ' ' ) ;
html = html.replace( /<SPAN\s*[^>]*><\/SPAN>/gi, '' ) ;
html = html.replace( /<(U|I|STRIKE|B|STRONG)>(.*)<\/\1>/ig, '$2' ) ;
// Remove empty tags (three times, just to be sure).
// This also removes any empty anchor
html = html.replace( /<([^\s>]+)(\s[^>]*)?>\s*<\/\1>/g, '' ) ;
html = html.replace( /<([^\s>]+)(\s[^>]*)?>\s*<\/\1>/g, '' ) ;
html = html.replace( /<([^\s>]+)(\s[^>]*)?>\s*<\/\1>/g, '' ) ;
return html;
} //html编码.
String.prototype.htmlEncode =function()
{
text = this;
text = text.replace(/&/g, "&") ;
text = text.replace(/"/g, """) ;
text = text.replace(/</g, "<") ;
text = text.replace(/>/g, ">") ;
text = text.replace(/'/g, "'") ; return text ;
}
//html解码.
String.prototype.htmlDecode =function()
{ text = this;
text = text.replace(/&/g, "&") ;
text = text.replace(/"/g, "\"") ;
text = text.replace(/</g, "<") ;
text = text.replace(/>/g, ">") ;
text = text.replace(/'/g, "'") ;
return text ;
}