1

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?

user1989
  • 163
  • 7
  • @Justcode in my case I need may have very big String may contain more repetitions. I know the first and end string and need to put in between. Please guide me!! – user1989 Sep 12 '15 at 06:02
  • @Justcode it is replacing the first or second occurrence and so on. In my case I need replace the value between two strings. hope you get it now!! – user1989 Sep 12 '15 at 06:07
  • http://stackoverflow.com/questions/14832239/replacing-text-between-two-limts does this help? – Just code Sep 12 '15 at 06:14
  • this previously answered link might be helpful- http://stackoverflow.com/a/9598725/5315670 – Gautam Sep 12 '15 at 06:17

3 Answers3

1

I would suggest preg_replace_callback. Check the code below.

function customReplace($string, $start, $end, $value = "NEWVALUE"){
    return preg_replace_callback('/'.$start.'(.*?)'.$end.'/', function($m) use($start, $end){
        return "$start NEWVALUE $end";
    }, $string);
}

$var = "Hello Hi World it is an awesome day. Test Hi guys Whats up";

echo customReplace($var, "Hello", "World");

Output:

Hello NEWVALUE World it is an awesome day. Test Hi guys Whats up

Problem with your code is:

$varnew = str_replace($parsed0,"NEWVALUE", $var);

$parsed0 has Hi (which you fetched from the function), and saying str_replace to replace Hi with NEWVALUE in the string $var(whole string). So str_replace is working as expected. Hope it is clear now.

UPDATE

Also check @b263 answer below. You can use preg_replace too, it doesn't require a callback.

Jigar
  • 3,256
  • 1
  • 30
  • 51
0

Hello NEWVALUE World it is an awesome day. Test Hi guys Whats up.

You're not going to be able to do that with str_replace.

$var = "Hello Hi World it is an awesome day. Test Hi guys Whats up";
//$parsed0 = 'Hi';
$varnew = str_replace($parsed0,"NEWVALUE", $var);

Because $parsed0 = 'Hi', any occurrence of 'Hi'in that string is going to be replaced. If you just want to replace the first occurrence, you could do the following

$varnew = preg_replace('/'.$parsed0.'/', ' NEWVALUE ', $var, 1);
user1104854
  • 2,137
  • 12
  • 51
  • 74
0
function replaceBetween($start, $end, $replacement, $subject) {
  return preg_replace("/(${start}\s+).*?(\s+${end})/", "$1${replacement}$2", $subject);
}

$var = 'Hello Hi World it is an awesome day. Test Hi guys Whats up';

echo replaceBetween('Hello', 'World', 'NEWVALUE', $var);
Troublesometimes
  • 462
  • 3
  • 12