在表单中,输入很多文本信息。
但是存入数据库中,在显示出来,有很多多于字符串(例如:&nbsp;<br>等等)
这是由于存入数据库中,把文本进行格式化,以后显示出来就正确了!
明白吗?

解决方案 »

  1.   

    把数据存入数据库前调用一下这个函数function clean_value($val) {
    if ($val == "")
         {
         return "";
         }
    $val = trim($val);
    $val = str_replace( "&#032;"       , " "             , $val );
    $val = str_replace( "&"            , "&amp;"         , $val );
    $val = str_replace( "<!--"         , "&#60;&#33;--"  , $val );
    $val = str_replace( "-->"          , "--&#62;"       , $val );
    $val = preg_replace( "/<script/i"  , "&#60;script"   , $val );
    $val = str_replace( ">"            , "&gt;"          , $val );
    $val = str_replace( "<"            , "&lt;"          , $val );
    $val = str_replace( "\""           , "&quot;"        , $val );
    $val = preg_replace( "/\|/"        , "&#124;"        , $val );
    $val = preg_replace( "/\n/"        , "<br>"          , $val ); // Convert literal newlines
    $val = preg_replace( "/\\\$/"      , "&#036;"        , $val );
    $val = preg_replace( "/\r/"        , ""              , $val ); // Remove literal carriage returns
    $val = str_replace( "!"            , "&#33;"         , $val );
    $val = str_replace( "'"            , "&#39;"         , $val ); // IMPORTANT: It helps to increase sql query  afety.
    $val = stripslashes($val);                                     // Swop PHP added backslashes
    $val = preg_replace( "/\\\/"       , "&#092;"        , $val ); // Swop user inputted backslashes
    return $val;
        }
      

  2.   

    $val = preg_replace( "/\n/", "<br>", $val );
      

  3.   

    不行。
    现在回车还是出现<br>
    其他的没有问题
      

  4.   

    字串处理函数,Encode用于存入数据库前使用,Decode于表单中的value中使用 /**
    +--------------------------------------------------
    | 函数名:Encode($str)
    | 作用:转换html代码和转行等。
    | 参数:
    | @param: $str:要转换的字符串
    | 返回值:转换后的字符串。
    +--------------------------------------------------
    */
    function Encode($str){
    if(!get_magic_quotes_gpc()){
    $str = addslashes($str);
    }
    $str = htmlspecialchars($str);
    $str = str_replace("\r\n","<br>",$str);
    $str = str_replace("\r","<br>",$str);
    $str = str_replace("\n","<br>",$str);
    $str = str_replace(" "," ",$str);
    $str = str_replace("'","’",$str);
    return $str;
    }
    /**
    +--------------------------------------------------
    | 函数名:Decode($str)
    | 作用:与Encode相反,用于修改时还原回本来的字符串
    | 参数:
    | @param: $str:要转换的字符串。
    | 返回值:转换后的字符串。
    +--------------------------------------------------
    */
    function Decode($str){
    $str = str_replace("<br>","\r\n",$str);
    $str = str_replace("<br>","\r",$str);
    $str = str_replace("<br>","\n",$str);
    $str = str_replace("<","&lt;",$str);
    $str = str_replace(">","&gt;",$str);
    $str = str_replace("’","'",$str);
    return $str;
    }