6

I have a string of that displays like this:

1235, 3, 1343, 5, 1234, 1

I need to replace every second comma with a semicolon

i.e.

1235, 3; 1343, 5; 1234, 1

The string length will always be different but will follow the same pattern as the above i.e. digits comma space digits comma space, etc.

How can I do this with PHP? Is it possible?

Thanks.

Robert
  • 19,800
  • 5
  • 55
  • 85
Jack Php
  • 577
  • 2
  • 7
  • 25
  • 3
    Of course it's possible, trivial even. Since this is a site for *professional or enthusiast programmers*, you should be able to figure out one of many possible ways in a few minutes. – deceze May 22 '13 at 08:30
  • 4
    You could try something like: `$replaced = preg_replace('/(.*),(.*),/', '$1,$2;', $strToReplace);` And I agree with @deceze. You should try to solve your problems on your own. – Leri May 22 '13 at 08:31
  • i agree there are some many ways to achieve this! You should attempt to solve this, then maybe come back to us and ask if there is a more efficient way of doing it. – MadDokMike May 22 '13 at 08:32
  • check my preg_replace solution it is the shortest and read about this function because it's really powerful when it goes to REGEX. – Robert May 22 '13 at 08:43
  • 1
    Oops, my bad. Pattern should be `/(.*?),(.*?),/` instead of `/(.*),(.*),/`. – Leri May 22 '13 at 14:01

6 Answers6

10

Preg_replace() solution

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

Output:

1235, 3; 1343, 5; 1234, 1
Robert
  • 19,800
  • 5
  • 55
  • 85
7

Try this :

$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);
Prasanth Bendra
  • 31,145
  • 9
  • 53
  • 73
4

Try this:

<?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;
}
?>

Result:

 1235, 3; 1343, 5; 1234, 1 
Avatar
  • 14,622
  • 9
  • 119
  • 198
Vijaya Pandey
  • 4,252
  • 5
  • 32
  • 57
2

Try this:

$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);

It outputs:

1235, 3; 1343, 5; 1234, 1
Bogdan Burym
  • 5,482
  • 2
  • 27
  • 46
1

try this:

$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;
k102
  • 7,861
  • 7
  • 49
  • 69
1

Try this:

<?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;
?>
corn on the cob
  • 2,093
  • 3
  • 18
  • 29
sAnS
  • 1,169
  • 1
  • 7
  • 10