1

Assuming I have a string

$str="0000,1023,1024,1025,1024,1023,1027,1025,1024,1025,0000";

there are three 1024, I want to replace the third with JJJJ, like this :

output :

0000,1023,1024,1025,1024,1023,1027,1025,JJJJ,1025,0000

how to make str_replace can do it

thanks for the help

ivanichi
  • 49
  • 9
  • Not sure you can do it with `str_replace`, you could use `explode` and `implode` to do it though. – Pitchinnate Apr 26 '13 at 15:18
  • Well, you could indeed, calling twice `str_replace` – Havelock Apr 26 '13 at 15:18
  • 1
    This should answer your question: http://stackoverflow.com/questions/3835636/php-replace-last-occurence-of-a-string-in-a-string – LeonardChallis Apr 26 '13 at 15:21
  • @Pitchinnate, hi pitchinnate, thanks, problem has been solved, your code is correct, thanks for the help. But, what if the count is not the same three words, but from the number of words that are, in case there is a position in 1024 at number nine ? – ivanichi Apr 26 '13 at 16:13
  • @Pitchinnate, thanks to your help. problem has been solved. – ivanichi Apr 26 '13 at 22:41

7 Answers7

1

As your question asks, you want to use str_replace to do this. It's probably not the best option, but here's what you do using that function. Assuming you have no other instances of "JJJJ" throughout the string, you could do this:

$str = "0000,1023,1024,1025,1024,1023,1027,1025,1024,1025,0000";
$str = str_replace('1024','JJJJ',$str,3)
$str = str_replace('JJJJ','1024',$str,2);
Jeff
  • 6,643
  • 3
  • 25
  • 35
  • Works in this case but what if 'JJJJ' exists in the `$str` originally. – Pitchinnate Apr 26 '13 at 15:28
  • Just use a different string that's longer and more diverse from the data set he's working with. Then add in another `str_replace` to replace that unique "identifier" string with "JJJJ" – Jeff Apr 26 '13 at 15:49
0

strpos has an offset, detailed here: http://php.net/manual/en/function.strrpos.php

So you want to do the following:

1) strpos with 1024, keep the offset

2) strpos with 1024 starting at offset+1, keep newoffset

3) strpos with 1024 starting at newoffset+1, keep thirdoffset

4) finally, we can use substr to do the replacement - get the string leading up to the third instance of 1024, concatenate it to what you want to replace it with, then get the substr of the rest of the string afterwards and concatenate it to that. http://www.php.net/manual/en/function.substr.php

Patashu
  • 21,443
  • 3
  • 45
  • 53
0

You can either use strpos() three times to get the position of the third 1024 in your string and then replace it, or you could write a regex to use with preg_replace() that matches the third 1024.

Håkan
  • 186
  • 6
0

if you want to find the last occurence of your string you can used strrpos

Nagasaki
  • 60
  • 2
  • 16
0

Here's a solution with less calls to one and the same function and without having to explode, iterate over the array and implode again.

// replace the first three occurrences
$replaced = str_replace('1024', 'JJJJ', $str, 3);
// now replace the firs two, which you wanted to keep
$final = str_replace('JJJJ', '1024', $replaced, 2);
Havelock
  • 6,913
  • 4
  • 34
  • 42
  • Works in this case but what if 'JJJJ' exists in the `$str` originally. – Pitchinnate Apr 26 '13 at 15:28
  • Well, the OP should know whether `JJJJ` may occur as a substring. If there's the remote possibility, then he/she can use other string as replacement, e.g. `§§§§`, `%%%%` or even `§%§%` or something similar... – Havelock Apr 26 '13 at 15:40
  • How is this helpful if he does not know how many occurrences of `1024` there are? – le3th4x0rbot Apr 27 '13 at 18:14
0

Do it like this:

$newstring = substr_replace($str,'JJJJ', strrpos($str, '1024'), strlen('1024') );

See working demo

Nelson
  • 49,283
  • 8
  • 68
  • 81
  • if there is the same three words, how the code is running eg : if we want to change the second, is there a number to allow us to replace only the first instance, only the second, third only ? – ivanichi Apr 26 '13 at 15:44
  • Yep, `strrpos` is a great alternative. Unfortunately the OP hasn't specified whether their `x-position` is the last possible occurrence. – Havelock Apr 26 '13 at 15:44
  • @ivanichi, [`strrpos`](http://www.php.net/manual/en/function.strrpos.php) returns the position of the last occurrence of the substring you're searching for. – Havelock Apr 26 '13 at 15:46
  • @Havelock, Thank you for your suggestion friend, pitchinnate have helped this, may be useful for us – ivanichi Apr 26 '13 at 15:56
0

Here is what I would do and it should work regardless of values in $str:

function replace_str($str,$search,$replace,$num) {
    $pieces = explode(',',$str);
    $counter = 0;
    foreach($pieces as $key=>$val) {
        if($val == $search) {
            $counter++;
            if($counter == $num) {
                $pieces[$key] = $replace;
            }
        }
    }
    return implode(',',$pieces);
}

$str="0000,1023,1024,1025,1024,1023,1027,1025,1024,1025,0000";

echo replace_str($str, '1024', 'JJJJ', 3);

I think this is what you are asking in your comment:

function replace_element($str,$search,$replace,$num) {
    $num = $num - 1;
    $pieces = explode(',',$str);
    if($pieces[$num] == $search) {
        $pieces[$num] = $replace;
    }
    return implode(',',$pieces);
}
$str="0000,1023,1024,1025,1024,1023,1027,1025,1024,1025,0000";
echo replace_element($str,'1024','JJJJ',9);
Pitchinnate
  • 7,517
  • 1
  • 20
  • 37
  • hi pitchinnate, thanks, problem has been solved, your code is correct, thanks for the help. – ivanichi Apr 26 '13 at 15:51
  • 1
    but, what if the count is not the same three words, but from the number of words that are, in case there is a position in 1024 at number nine ? – ivanichi Apr 26 '13 at 16:03
  • So your saying not necessarily the 3rd instance of 1024, but you want to check the 9th element in the array to check if it is 1024? If it is then replace it? – Pitchinnate Apr 26 '13 at 17:03
  • @ivanichi if I understood the question in your comment correctly I updated my answer to what I think you are asking for. – Pitchinnate Apr 26 '13 at 17:26
  • your code is correct, i think this is the solution, the problem has been solved. you really helped me. thanks to your help. – ivanichi Apr 26 '13 at 22:39