1

I have multiple strings and I want to change the string which is between first and the last string.

The middle string is dynamic and it changes but the first and the last string remains the same.

For example this is the string,

Please note that this is the entire string.

%22%3A%7B%22id%22%3A%22XXXXXXXXXXXXXXXXXXXXXX%22%7D%2C%22 %22%3A%7B%22id%22%3A%22YYYYYYYYYYYYYYYYYYYYYY%22%7D%2C%22 %22%3A%7B%22id%22%3A%22ZZZZZZZZZZZZZZZZZZZZZZ%22%7D%2C%22

So is there any function which can find all the middle string which has the same first and the last string ?

I tried this function,

private function replace_between($str, $needle_start, $needle_end, $replacement)
    {
    
        $pos = strpos($str, $needle_start);

        $start = $pos === false ? 0 : $pos + strlen($needle_start);
    
        $pos = strpos($str, $needle_end, $start);
        $end = $pos === false ? strlen($str) : $pos;
    
        return substr_replace($str, $replacement, $start, $end - $start);
    }

This function only replaces the first occurrence. I want to change all the occurrences.

Output of this function

%22%3A%7B%22id%22%3A%22REPLACED___TEXT%22%7D%2C%22
%22%3A%7B%22id%22%3A%22YYYYYYYYYYYYYYYYYYYYYY%22%7D%2C%22
%22%3A%7B%22id%22%3A%22ZZZZZZZZZZZZZZZZZZZZZZ%22%7D%2C%22

It is doesn't change the other middle strings.

Thanks!

6
  • please provide example of how you call the replace_between() Commented Dec 17, 2020 at 5:15
  • if the strings are separated by a space (looks like so in the example provided), you could explode it using the space as a separator. The second element of the exploded array should give the second string.. Commented Dec 17, 2020 at 5:17
  • @ariefbayu eplace_between(ORIGINAL_STRING,"%22%3A%7B%22id%22%3A%","%22%7D%2C%22","REPLACETEXT") This replaces the first occurrence but all other occurrences doest change Commented Dec 17, 2020 at 5:18
  • @user3532758 What are you talking about ? I didn't get it. I want to replace the string between 2 strings. Why would I explode it ? Commented Dec 17, 2020 at 5:21
  • so, suppose you have 3 lines of those, you want to replace EVERYTHING in between those 3 lines, with only one ""REPLACE TEXT"? I mean, all three lines will have the same content. @AbhishekDeshkar Commented Dec 17, 2020 at 5:23

2 Answers 2

1

You can use preg_replace() for that:

<?php
// Your code here!
$string = "%22%3A%7B%22id%22%3A%22XXXXXXXXXXXXXXXXXXXXXX%22%7D%2C%22 %22%3A%7B%22id%22%3A%22YYYYYYYYYYYYYYYYYYYYYY%22%7D%2C%22 %22%3A%7B%22id%22%3A%22ZZZZZZZZZZZZZZZZZZZZZZ%22%7D%2C%22";

$replaceInto = "---REPLACED---";
$pattern = '/\%22\%3A\%7B\%22id\%22\%3A\%22(.*)\%22\%7D\%2C\%22/U';

$result = preg_replace($pattern, "%22%3A%7B%22id%22\%3A%$replaceInto%22%7D%2C%22", $string);

var_dump($result);

Notice that I used suffix U in the $pattern. This is called 'Nongreedy' modifier.

It would result in the following:

string(176) "%22%3A%7B%22id%22%3A%---REPLACED---%22%7D%2C%22 %22%3A%7B%22id%22%3A%---REPLACED---%22%7D%2C%22 %22%3A%7B%22id%22%3A%---REPLACED---%22%7D%2C%22"

Here is the online test on Paiza.IO.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot! It worked. However there is a change here, it should be like this. preg_replace($pattern,"%22%3A%7B%22id%22%3A%22$replaceInto%22%7D%2C%22", $string);
1

@user3532758 What are you talking about ? I didn't get it. I want to replace the string between 2 strings. Why would I explode it ?

$string = "%22%3A%7B%22id%22%3A%22XXXXXXXXXXXXXXXXXXXXXX%22%7D%2C%22 %22%3A%7B%22id%22%3A%22YYYYYYYYYYYYYYYYYYYYYY%22%7D%2C%22 %22%3A%7B%22id%22%3A%22ZZZZZZZZZZZZZZZZZZZZZZ%22%7D%2C%22";

$exploded = explode(" ", $string);

$exploded[1] = "replaced string";

echo implode($exploded, " ");

// result: %22%3A%7B%22id%22%3A%22XXXXXXXXXXXXXXXXXXXXXX%22%7D%2C%22 replaced string %22%3A%7B%22id%22%3A%22ZZZZZZZZZZZZZZZZZZZZZZ%22%7D%2C%22

1 Comment

Why did you add the space ? I have edited the question. Please look into it again.

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.