有一个变量:$aaa = "1,2,3,6,20,7,19,8";如果我想删除变量中的“6,”我该怎么写代码?谢谢,,,请详细一些

解决方案 »

  1.   


    $arr = array(1,2,3,6,20,7,19,8);
    foreach($arr as $iKey=>$iValue) {

    if($iValue == 6)
    unset($arr[$iKey]);
    }
    print_r($arr);
      

  2.   


    $sString = "1,2,3,6,20,7,19,8";
    echo substr($sString, 0, strpos($sString, '6,',0)) . substr($sString, strpos($sString, '6,',0)+2);
      

  3.   


    $sString = "1,2,3,6,20,7,19,8";
    //echo substr($sString, 0, strpos($sString, '6,',0)) . substr($sString, strpos($sString, '6,',0)+2);
    $sString = str_ireplace('6,', '', $sString);
    echo $sString;
      

  4.   

    修正楼上的代码:$sString = "1,2,3,6,20,7,19,8";
    $sString = trim(str_replace(',6,', ',', ','.$sString.','),',');
    echo $sString;
      

  5.   

    再简化一下
    $sString = "1,2,3,6,20,7,19,8";
    $sString = trim(str_replace('6,', '', $sString.','),',');
    echo $sString;
      

  6.   


    $sString = "1,2,3,6,20,7,19,8";
    //echo substr($sString, 0, strpos($sString, '6,',0)) . substr($sString, strpos($sString, '6,',0)+2);
    $sString = str_ireplace('6,', '', $sString);
    //$sString = preg_replace('@6,@', '', $sString);
    echo $sString;
    1,2,3,20,7,19,8
      

  7.   

    是吗?
    $sString = "6,1,2,3,6,20,,16,7,19,8,6";
    //echo substr($sString, 0, strpos($sString, '6,',0)) . substr($sString, strpos($sString, '6,',0)+2);
    $sString = str_ireplace('6,', '', $sString);
    echo $sString;
    out: 1,2,3,20,,17,19,8,6
      

  8.   

    看你的,未必完整
    $sString = "6,1,2,3,6,20,,16,7,19,8,6,";
    //echo substr($sString, 0, strpos($sString, '6,',0)) . substr($sString, strpos($sString, '6,',0)+2);
    //$sString = str_ireplace('6,', '', $sString);
    $sString = trim(str_replace('6,', '', $sString.','),',');
    echo $sString;
    out:1,2,3,20,,17,19,8
      

  9.   

    这种也不是没有可能:
    $sString = ",6,1,2,3,6,20,,16,7,19,8,6";
    //echo substr($sString, 0, strpos($sString, '6,',0)) . substr($sString, strpos($sString, '6,',0)+2);
    //$sString = str_ireplace('6,', '', $sString);
    $sString = trim(str_replace('6,', '', $sString.','),',');
    echo $sString;
    //out:1,2,3,20,,17,19,8
      

  10.   

    这个字符串$sString = ",6,1,2,3,6,20,,16,7,19,8,6";你想要多少种
      

  11.   

    方法有很多,选择什么方法需要根据程序的切实需求,
    我倒是有个比较笨的方法可以满足楼上所说的各种情况:$str = "6,1,2,3,6,20,,16,7,19,8,6";
    $str_arr = explode(',',$str);
    foreach ($str_arr as $key=>$value) {
    if (6 == $value) {
    unset($str_arr[$key]);
    }
    }
    $str = implode(',',$str_arr);
    echo $str;out:1,2,3,20,,16,7,19,8
      

  12.   


    $str = "6,1,2,3,6,20,,16,7,19,8,6";
    $str_arr = explode(',',$str);
    foreach ($str_arr as $key=>$value) {
    if (6 == $value) {
    unset($str_arr[$key]);
    }
    }
    $str = implode(',',$str_arr);
    echo $str;
    out:1,2,3,20,,16,7,19,8
    他要删除6,你把最后的6删除了
      

  13.   

    一个变量:$aaa = "1,2,3,6,20,7,19,8";
    $reg_rule = '/.6./'
    while(strpos($aaa) != false)
    {
        str_replace($reg_rele, '', $aaa);
    }
    echo $aaa;
      

  14.   

    这又当如何?$sString = "6,6,6,61,2,3,6,20,7,19,8";
    $sString = trim(str_replace(',6,', ',', ','.$sString.','),',');
    echo $sString;
      

  15.   


    $sString = "1,2,3,6,20,7,19,8";
    $sString = trim(str_replace(',6,', ',', ','.$sString.','),',');
    echo $sString;
    如果6 是第一位呢????$str = "6,1,2,3,6,20,,16,7,19,8,6";
    $str_arr = explode(',',$str);
    foreach ($str_arr as $key=>$value) {
    if (6 == $value) {
    unset($str_arr[$key]);
    }
    }
    $str = implode(',',$str_arr);
    echo $str;
    out:1,2,3,20,,16,7,19,8
    他要删除6,你把最后的6删除了把这个修修就好了。$str = "6,1,2,3,6,20,,16,7,19,8,6";
    $str_arr = explode(',',$str);
    foreach ($str_arr as $key=>$value) {if (6 == $value && $key < sizeof($str_arr) - 1 ) {
    unset($str_arr[$key]);
    }
    }
    $str = implode(',',$str_arr);
    echo $str;
      

  16.   

    用数组方法处理一下:<?php
    $aaa = "1,2,3,6,20,7,19,8";
    echo implode(',',array_diff(explode(',',$aaa),array(6)));
    ?>
      

  17.   

    $aaa = "1,2,3,20,7,19,8,6";
    echo implode(',',array_diff(explode(',',$aaa),array(6)));
    out:1,2,3,20,7,19,8
      

  18.   

    运用集合的运算,大家有考虑没。
    下面是代码:   $aaa = "1,2,3,6,20,7,6,19,8";
       $A=array();
       $A=explode(",",$aaa);
       $B=array(
        "6",
       );
       $result=array_diff($A,$B);
       print_r($result);