本帖最后由 uczone 于 2009-11-22 01:49:56 编辑

解决方案 »

  1.   

    $a=",张三,李四,王五,张飞,李四,,,张三,大 话,";echo str_replace(' ', '', trim($a, ","));
      

  2.   

    去掉中间多余的逗号和首尾的逗号$s=",张三,李四,王五,张飞,李四,,,张三,大 话,";
    preg_replace("/^,?([^,]{2,}),?$/g","$1,",$s);
      

  3.   

    $s=",张三,李四,王五,张飞,李四,,,张三,大 话,";
    preg_replace("/^,?([^,]{2,}),?$/g","$1,",trim($s,','));
      

  4.   

    <?php
    $str=",张三,李四,王五,张飞,李四,,,张三,大 话,";
    $a = explode(",",substr($str,1,strlen($str)-1));
    $b = array_unique($a);foreach($b as $k)
    {
        echo "<script>alert('$k')</script>";
    }
    ?>
      

  5.   

    去除多余的
    <?php
    $str=",张三,李四,王五,张飞,李四,,,张三,大 话,";
    $str=preg_replace('/[,]{2,}/',',',$str); 
    $a = explode(",",trim($str,','));
    $b = array_unique($a);
    foreach($b as $k)
    {
        echo "<script>alert('$k')</script>";
    }
    ?>
      

  6.   

    回复:chinmo 您好,
    您提供的已经能替换,但能实现,字符中间的空格也替换吗?谢谢
      

  7.   


    <?php
    $str=",张三,李四,王五,张飞,李四,,,张三,大 话,";
    $str=preg_replace('/[,]{2,}/',',',$str); 
    $str = preg_replace("/\s+/g","",$str);
    $a = explode(",",trim($str,','));
    $b = array_unique($a);
    foreach($b as $k)
    {
        echo "<script>alert('$k')</script>";
    }
    ?>
      

  8.   

    感谢以上各位网友的热心解答和回复,问题已经解决了:
    $str=",张三,李四,王五,张飞,李四,,,张三,大 话,";
    $str=preg_replace('/[,]{2,}/',',',$str); 
    $str = preg_replace("/\s/","",$str);
    $a = explode(",",trim($str,','));
    $b = array_unique($a);
    foreach($b as $k)
    {
        echo "<script>alert('$k')</script>";
    }