这个是将用户文章内容中的相关字符显示的时候替换掉。
用函数一样得一个个的写,麻烦。
可否用preg_replace或者数组替换那样实现?

解决方案 »

  1.   

    <?php
    $str = ":)";
    echo repStr($str);
    function repStr($post){
    $post=str_replace("[wdb2]","<img src='images/pic/wdb2.gif' align=top>",$post);
    $post=str_replace("[wdb3]","<img src='images/pic/wdb3.gif' align=top>",$post);
    $post=str_replace("[wdb4]","<img src='images/pic/wdb4.gif' align=top>",$post);
    $post=str_replace("[wdb5]","<img src='images/pic/wdb5.gif' align=top>",$post);
    $post=str_replace("[wdb6]","<img src='images/pic/wdb6.gif' align=top>",$post);
    $post=str_replace("[wdb7]","<img src='images/pic/wdb7.gif' align=top>",$post);
    $post=str_replace("[wdb8]","<img src='images/pic/wdb8.gif' align=top>",$post);
    $post=str_replace("[wdb9]","<img src='images/pic/wdb9.gif' align=top>",$post);
        $post=str_replace(":)","<img src='images/pic/smile.gif' align=top>",$post);
        $post=str_replace(":D","<img src='images/pic/laugh.gif' align=top>",$post);
    $post=str_replace(":love:","<img src='images/pic/love.gif' align=top>",$post);
    $post=str_replace(":cool:","<img src='images/pic/cool.gif' align=top>",$post);
    return $post;
    }?>
      

  2.   

    我晕。我就是说那个定义里面能不能不用写那么多,比如wdbn那些都是有规律的,而且总体来说也是有规律的。做成一个array那样,以后便可以方便增加更多的了。
      

  3.   

    就是说想动态添加删除表情符号……
    存在数组库里或是文本里需要时读一下就是了。。
    看vbb源码。
      

  4.   

    $a=array("\[wdb([0-9*])\]",":)",……);
    $b=array("\\1","smile",……);
    $var=preg_replace($a,$b,$var);
    这样可以吧?然后再把得出的$var进入循环中,给每一个值加上<img>的套,再和文章内容进行str_replace替换。这样可以大大简化很多很多不同的表情符号组成的替换式子。这样的上式和思路怎样?
      

  5.   

    我在我们的网站写了一个正则表达式:按日期播放音乐,music1,music2,....
    看看是否满足你的需要
    http://www.aweboa.com.cn在主页中你点击菜单---查看源文件
      

  6.   

    谢谢。那一方面的?我想要php的。好像不是的吧?
      

  7.   

    function mycode($tmp){
    $from=array("/\[wdb([0-9]+?)\]/is","/:\)/is","/:D/is","/:love/is","/:cool/is");
    $to=array("<img src=\"images/pic/wdb\\1".".gif\" align=top>",
    "<img src='images/pic/smile.gif' align=top>",
    "<img src='images/pic/laugh.gif' align=top>",
    "<img src='images/pic/love.gif' align=top>",
    "<img src='images/pic/cool.gif' align=top>");
    $tmp=preg_replace($from,$to,$tmp);
    return $tmp;
    }使用的方法就是$post=mycode($post);
      

  8.   

    后面那几个需要用/is 么?它们又不用怎样匹配,直接str_replace不行么?
      

  9.   

    /**
    *
    * 作者:偶然
    * 功能:字符串替换
    * 时间:2003.7.5
    * 变量:
    *   code: 替换码,可自己在数组或是数据库中自定义
    *   pic: 图片路径,可以从数组或是数据库中取得
    *   post: 传入的字符串
    * 返回:
    *   post: 替换后的代码
    * 示例:
    *
    */
    function replace_codes($code,$pic,$post)
    {
    $post=str_replace($code,$pic,$post);
    Return $post;
    }
      

  10.   

    $post=str_replace("[wdb9]","<img src='images/pic/wdb9.gif' align=top>",$post);
    $post=str_replace("[wdb10]","<img src='images/pic/wdb10.gif' align=top>",$post);
    $post=str_replace("[wdb11]","<img src='images/pic/wdb11.gif' align=top>",$post);
    $post=str_replace(":xigua:","<img src='images/pic/xigua.gif' align=top>",$post);
    $post=str_replace(":cake:","<img src='images/pic/cake.gif' align=top>",$post);
    $post=str_replace(":upig:","<img src='images/pic/upig.gif' align=top>",$post);
    $post=str_replace(":uphone:","<img src='images/pic/uphone.gif' align=top>",$post);
    $post=str_replace(":food:","<img src='images/pic/food.gif' align=top>",$post);
    $post=str_replace(":cry:","<img src='images/pic/cry.gif' align=top>",$post);
    $post=str_replace(":shy:","<img src='images/pic/shy.gif' align=top>",$post);
    $post=str_replace(":phone:","<img src='images/pic/phone.gif' align=top>",$post);如上面的内容,要替换的wdb部分的描述是"/\[wdb([0-9]+?)\]"这个没错的吧?
    除了替换:D等内容,其他大部分都是直接英文单词变成文件名的,所以我想用这个方式:
    $a=array(
    "/\[wdb([0-9]+?)\]/is",
    "/:\)/is",
    "^:+(/w*)+:$/U"
              ~~~~~~~~~~~~~这个正则可能不对,是说以:开头和结尾的其中有小于8个字母的单词就替换成没有冒号的
    );
    $b=array(
    wdb\\1,
    smile,
    \\1
    );
    然后给$b数组加上<img src='images/pic/*.gif' align=top>的套再进行$post=preg_replace($a,$b,$post);。这样就可以少写很多字了。问题有三:
    第一、上面那个正则怎样写?
    第二、$b数组需不需要每一项加上引号?
    第三、怎样给数组的每一项加上套?只能遍历?