I have a String as follows:
$var = "Hello Hi World it is an awesome day. Test Hi guys Whats up";
I am trying to replace the value in my string as follows:
function get_string_between($string, $start, $end){
$string = " ".$string;
$ini = strpos($string,$start);
if ($ini == 0) return "";
$ini += strlen($start);
$len = strpos($string,$end,$ini) - $ini;
return substr($string,$ini,$len);
}
$parsed0 = get_string_between($var, "Hello", "World");
So I get the String in `$parse0`
And I am replacing as follows:
$varnew = str_replace($parsed0,"NEWVALUE", $var);
echo $varnew;
I get output as follows:
Hello NEWVALUE World it is an awesome day. Test NEWVALUE guys Whats up
My expected result is:
Hello NEWVALUE World it is an awesome day. Test Hi guys Whats up.
This is happening because in $parse0
I get result as Hi
and it contains two times in that String.
Is there any way I get start and and end positions using a function and replace it using those?