需要处理字符 ,< 到 &gt 等的转换

解决方案 »

  1.   

    把不要的都过滤掉.
    或者显示的时候做html编码.
      

  2.   


    //区域框特殊标记替换
    function strHtml($str)
    {
    if(empty($str)) return false;
    $str=str_replace('<','&lt;',$str);
    $str=str_replace('>','&gt;',$str);
    $str=str_replace("\n",'<br>',$str);
    $str=str_replace(" ",'&nbsp;',$str);
    return $str;
    }//区域框特殊标记还原
    function htmlStr($str)
    {
    if(empty($str)) return false;
    $str=str_replace('&lt;','<',$str);
    $str=str_replace('&gt;','>',$str);
    $str=str_replace("<br>","\n",$str);
    $str=str_replace("&nbsp;",' ',$str);
    return $str;
    }
      

  3.   

    没用过!
    strp_tags():去除<和>标签及当中的内容
    addslashes():给五个字符转义,防止mysql误读
    htmlspecialchars():不知道建议你看编辑器源码:看其对文本框的处理过程。如没有处理,可按以下常规处理:
    第一种方法:PHP端处理:判断应用addslashes(),这只是入库,出库用stripslashes()
       注意:入库和出库的判断设置变量不一样第二中:如果有源码显示
       可以将用js或PHP端进行替换,将<和>转换成特别代码,显示时回转:< 到 &gt 转换就是这个意思
      

  4.   

    取值这样取if ( get_magic_quotes_gpc() ) {
        $fck_value = htmlspecialchars(stripcslashes($_POST['fck']));
       } else {
        $fck_value = htmlspecialchars($_POST['fck']);
       }从数据库取值的时候htmlspecialchars_decode($mysql_fck_vaule);
      

  5.   

    讲讲我在碰到这类问题的处理方式吧.
    通常我用的是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*&nbsp;\s*<\/SPAN>/gi, '&nbsp;' ) ;
    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, "&amp;") ;
    text = text.replace(/"/g, "&quot;") ;
    text = text.replace(/</g, "&lt;") ;
    text = text.replace(/>/g, "&gt;") ;
    text = text.replace(/'/g, "&#39;") ; return text ;
    }
    //html解码.
     String.prototype.htmlDecode =function()
     { text = this;

    text = text.replace(/&amp;/g, "&") ;
    text = text.replace(/&quot;/g, "\"") ;
    text = text.replace(/&lt;/g,  "<") ;
    text = text.replace(/&gt;/g, ">") ;
    text = text.replace(/&#39;/g, "'") ;
        return text ;
    }