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.

5
  • 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. Commented May 22, 2013 at 8: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. Commented May 22, 2013 at 8: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. Commented May 22, 2013 at 8: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. Commented May 22, 2013 at 8:43
  • 1
    Oops, my bad. Pattern should be /(.*?),(.*?),/ instead of /(.*),(.*),/. Commented May 22, 2013 at 14:01

6 Answers 6

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
Sign up to request clarification or add additional context in comments.

2 Comments

This is by far the best solution
This is the shortest solution to the problem ;)
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);

Comments

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 

1 Comment

Be aware that $find and $replace can only be one character long.
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

Comments

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;

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.