需要处理字符 ,< 到 > 等的转换
解决方案 »
- 高手进!!!!!!!
- 我写的一个统计访问状况的代码,发现有并发问题,请专家们进来看看!
- 问个问题??
- 怎么写php可以登录一个邮件服务器
- 再次研究正则表达式
- 点击下载的问题
- 討論咨詢!! 寫网頁時寫 HTML 靜態頁面是很重要的,而且要寫的也很多,是不?? 不管用 PHP 還是 ASP 來達到動態都要用 HTML 靜態頁來表示結果
- 怎样简体转繁体!
- php自定义函数 ,调用一些mysql函数 老是提示错误
- 【数据合并】实现一个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 ;
}