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!
replace_between()explodeit using the space as a separator. The second element of the exploded array should give the second string..