替换...
s = replace(s,space(1)," ")

解决方案 »

  1.   

    $Text=ereg_replace(" "," ",$Text);
    $Text//是你的textarea
      

  2.   

    读出来时,加上htmlspecialchars(变量名)就可以了
      

  3.   

    呵呵,挺热闹啊,那大家看看这个函数
    <?
    function ubb($tmpmsg) {
    //        $tmpmsg =str_replace('&lt;','<',$tmpmsg);
    //        $tmpmsg =str_replace('&gt;','>',$tmpmsg);
            $tmpmsg=htmlspecialchars($tmpmsg);
            $tmpmsg=nl2br($tmpmsg);
            //$tmpmsg =str_replace('','&nbsp;',$tmpmsg);
            $tmpmsg =str_replace('&amp;','&',$tmpmsg);
            $tmpmsg =str_replace("","<u>",$tmpmsg);
            $tmpmsg =str_replace("
    ","</u>",$tmpmsg);
            $tmpmsg =str_replace("","<b>",$tmpmsg);
            $tmpmsg =str_replace("","</b>",$tmpmsg);
            $tmpmsg =str_replace("","<i>",$tmpmsg);
            $tmpmsg =str_replace("
    ","</i>",$tmpmsg);
            $tmpmsg =str_replace("[br]","<br>",$tmpmsg);
            $tmpmsg =str_replace("[list]","<ul>",$tmpmsg);
            $tmpmsg =str_replace("[/list]","</ul>",$tmpmsg);
            $tmpmsg =str_replace("[olist]","<ol>",$tmpmsg);
            $tmpmsg =str_replace("[/olist]","</ol>",$tmpmsg);
            $tmpmsg =str_replace("[*]","<li>",$tmpmsg);
            $tmpmsg =str_replace("[hr]","<hr width=40% align=left>",$tmpmsg);
            $tmpmsg =str_replace("[sup]","<sup>",$tmpmsg);
            $tmpmsg =str_replace("[/sup]","</sup>",$tmpmsg);
            $tmpmsg =str_replace('[url=&quot;','[url="',$tmpmsg);
            $tmpmsg =str_replace('&quot;]','"]',$tmpmsg);        $pattern = array(
            "/\[font=([^\[]*)\](.+?)\[\/font\]/is",
            "/\[color=([#0-9a-z]{1,10})\](.+?)\[\/color\]/is",
            "/\[email=([^\[]*)\](.+?)\[\/email\]/is",
            "/\[email\]([^\[]*)\[\/email\]/is",
            "/\[url=([^\[]*)\](.+?)\[\/url\]/is",
            "/\[url\]www\.([^\[]*)\[\/url\]/is",
            "/\[url\]([^\[]*)\[\/url\]/is",
            "/\[quote\](.*)\[quote\](.*)\[\/quote](.*)\[\/quote\]/is",
            "/\[quote\]\s*(.*?)\s*\[\/quote\]/is",
            "/(\[fly\])(.+?)(\[\/fly\])/is",
            "/(\[move\])(.+?)(\[\/move\])/is",
            "/(\[align=)(left|center|right)(\])(.+?)(\[\/align\])/is",
            "/(\[shadow=)(\S+?)(\,)(.+?)(\,)(.+?)(\])(.+?)(\[\/shadow\])/is",
            "/(\[glow=)(\S+?)(\,)(.+?)(\,)(.+?)(\])(.+?)(\[\/glow\])/is",
            "/\[code\](.+?)\[\/code\]/is");        $replacement = array(
            "<font face=\"\\1\">\\2</font>",
            "<font color=\"\\1\">\\2</font>",
            "<a href=\"mailto:\\1\">\\2</a>",
            "<a href=\"mailto:\\1\">\\1</a>",
            "<a href=\"\\1\" target=_blank>\\2</a>",
            "<a href=\"http://www.\\1\" target=_blank>\\1</a>",
            "<a href=\"\\1\" target=_blank>\\1</a>",
            "<blockquote><hr noshade><font face=宋体, helvetica>\\1\\2<br>\\3</font><hr noshade></blockquote>",
            "<font face=$font><blockquote><hr noshade>\\1<hr noshade></blockquote></font>",
            "<marquee width=90% behavior=alternate scrollamount=3>\\2</marquee>",
            "<MARQUEE scrollamount=3>\\2</MARQUEE>",
            "<DIV Align=\\2>\\4</DIV>",
            "<table width=\\2 style=\"shadow(color=\\4, direction=\\6 ,strength=2)\">\\8</table>",
            "<table width=\\2 style=\"filter:glow(color=\\4, strength=\\6)\">\\8</table>",
            "<table border=0 width=95% align=center cellpadding=2 bgcolor=DDDDDF><tr><td><pre><font face='Courier New'>\\1</font></pre></td></tr></table>");        $tmpmsg=preg_replace($pattern,$replacement,$tmpmsg);            $tmpmsg = preg_replace("/\[img\]\s*(\S+?)\s*\[\/img\]/is","<img src=\\1 border=0>",$tmpmsg);            $tmpmsg = preg_replace("/(\[swf\])\s*(\S+?\.swf)\s*(\[\/swf\])/is","<PARAM NAME=PLAY VALUE=TRUE><PARAM NAME=LOOP VALUE=TRUE><PARAM NAME=QUALITY VALUE=HIGH><embed src=\"\\2\" quality=high pluginspage=\"http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash\" type=\"application/x-shockwave-flash\"></embed>",$tmpmsg);
                $tmpmsg = preg_replace("/(\[FLASH=)(\S+?)(\,)(\S+?)(\])(\S+?)(\[\/FLASH\])/is","<OBJECT CLASSID=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" WIDTH=\\2 HEIGHT=\\4><PARAM NAME=MOVIE VALUE=\\6><PARAM NAME=PLAY VALUE=TRUE><PARAM NAME=LOOP VALUE=TRUE><PARAM NAME=QUALITY VALUE=HIGH><EMBED SRC=\\6 WIDTH=\\2 HEIGHT=\\4 PLAY=TRUE LOOP=TRUE QUALITY=HIGH></EMBED></OBJECT>",$tmpmsg);
                $tmpmsg = eregi_replace("\\[size=([^\\[]*)\\]","<font size=\\1>",$tmpmsg);
                $tmpmsg = str_replace("[/size]","</font>",$tmpmsg);        return $tmpmsg;
    }
    ?>
      

  4.   

    然后我从数据库读出数据的时候用这个函数
    例如:
     <?
    include("inc/conn.php");
    include("inc/function.php");//其中有ubb函数
    $sql="select id,body from news where id='$id' order by id";
    $result=mysql_query($sql);
    if($myrow=mysql_fetch_array($result))
      {
              $id=$myrow["id"];
              $body=ubb($myrow["body"]);
    echo "$body";
    }
    ?>
    这样怎么不可以显示空格呢?
      

  5.   

    $body=str_replace(" ", "&nbsp;", $myrow["body"]);
      

  6.   

    楼上的方法没有做好。抗议楼上们,你们都说光了,我还说什么?不过你们漏了一点。嘿嘿。 ice_berg16(寻梦的稻草人) 的替换nbsp的方法解决不了问题。因为在html显示里,超过两个nbsp的空格代码就会自动忽略,显示不出来。呵呵。所以还是要替换成全角空格才行。楼主,你上面说显示不出来空格,给你个方法吧:
    $body=str_replace("  "," ", $myrow["body"]);注意,第一个是两个半角空格,就是常用的空格。第二个是一个全角空格。这样就能工整显示了。
      

  7.   

    用 htmlspecial($_POST['textareaName']) 取值就OK了
      

  8.   

    不好意思~~~~~`应该是 htmlspecialchars($_POST['textareaName']) 上面函数中少了chars *^_^*
      

  9.   

    htmlspecialchars这样不行吧?如果有多个空格,就会最多只显示两个的。