【问题标题】:replace every second comma of string using php使用php替换字符串的每隔一个逗号
【发布时间】:2013-05-17 05:18:39
【问题描述】:

我有一串这样的显示:

1235, 3, 1343, 5, 1234, 1

我需要用分号替换每隔一个逗号

1235, 3; 1343, 5; 1234, 1

字符串长度总是不同的,但会遵循与上面相同的模式,即数字逗号空格数字逗号空格等

我怎样才能用 PHP 做到这一点?有可能吗?

谢谢。

【问题讨论】:

  • 当然有可能,甚至是微不足道的。由于这是一个面向专业或狂热程序员的网站,您应该能够在几分钟内找出许多可能的方法之一。
  • 你可以尝试类似:$replaced = preg_replace('/(.*),(.*),/', '$1,$2;', $strToReplace); 我同意@deceze。您应该尝试自己解决问题。
  • 我同意有很多方法可以实现这一点!您应该尝试解决这个问题,然后可能会回到我们这里询问是否有更有效的方法。
  • 检查我的 preg_replace 解决方案,它是最短的并阅读有关此功能的信息,因为它在 REGEX 时非常强大。
  • 糟糕,我的错。模式应该是/(.*?),(.*?),/ 而不是/(.*),(.*),/

标签: php replace


【解决方案1】:

试试这个:

$s = "1235, 3, 1343, 5, 1234, 1";
$pcs = explode(',', $s);

$flag = false;
$res = '';
foreach ($pcs as $item) {
    if (!empty($res)) {
        $res .= $flag ? ',' : ';';
    }
    $flag = !$flag;
    $res .= $item;
}
die($res);

它输出:

1235, 3; 1343, 5; 1234, 1

【讨论】:

    【解决方案2】:

    试试这个:

    <?php
    $string =  '1235, 3, 1343, 5, 1234, 1';
    
    var_dump(nth_replace($string, ',', ';', 2));
    
    // replace all occurences of a single character with another character
    function nth_replace($string, $find, $replace, $n) {
            $count = 0;
            for($i=0; $i<strlen($string); $i++) {
                    if($string[$i] == $find) {
                            $count++;
                    }
                    if($count == $n) {
                            $string[$i] = $replace;
                            $count = 0;
                    }
            }
            return $string;
    }
    ?>
    

    结果:

     1235, 3; 1343, 5; 1234, 1 
    

    【讨论】:

    • 请注意$find$replace 只能是一个字符。
    【解决方案3】:

    试试这个:

    $s = '1235, 3, 1343, 5, 1234, 1';
    $is_second = false;
    for ($i = 0; $i < strlen($s); $i++) {
        if ($is_second && $s[$i] == ',') {
            $s[$i] = ';';
        } elseif ($s[$i] == ',') {
            $is_second = true;
        }
    }
    echo $s;
    

    【讨论】:

      【解决方案4】:

      试试这个:

      $str     = '1235, 3, 1343, 5, 1234, 1';
      $res_str = array_chunk(explode(",",$str),2);
      foreach( $res_str as &$val){
         $val  = implode(",",$val);
      }
      echo implode(";",$res_str);
      

      【讨论】:

        【解决方案5】:

        Preg_replace()解决方案

        $str = '1235, 3, 1343, 5, 1234, 1';
        $str = preg_replace('/(.+?),(.+?),/', '$1,$2;', $str);
        echo $str;
        

        输出:

        1235, 3; 1343, 5; 1234, 1
        

        【讨论】:

        • 这是迄今为止最好的解决方案
        • 这是问题的最短解决方案;)
        【解决方案6】:

        试试这个:

        <?php
            $str = "1235, 3, 1343, 5, 1234, 1";
            $data = explode(',',$str);
            $counter = 0;
            $new_str = "";
            foreach($data as $key=>$val) {
                if ($counter%2 == 0) {
                    $symbol=',';
                }
                else {
                    $symbol=';';
                }
                $new_str .= $val.$symbol; 
                $counter++;
            }
            echo $new_str;
            //output::1235, 3; 1343, 5; 1234, 1;
        ?>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-12-24
          • 2016-06-15
          • 2017-06-05
          • 1970-01-01
          • 2011-12-23
          • 1970-01-01
          • 2013-09-20
          相关资源
          最近更新 更多