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?

4
  • @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!! Commented Sep 12, 2015 at 6: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!! Commented Sep 12, 2015 at 6:07
  • stackoverflow.com/questions/14832239/… does this help? Commented Sep 12, 2015 at 6:14
  • this previously answered link might be helpful- stackoverflow.com/a/9598725/5315670 Commented Sep 12, 2015 at 6:17

3 Answers 3

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.

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

Comments

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);

Comments

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);

Comments

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.