String Replace in php not working as expected

user1989

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?

Jigar

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.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

C++ String->Replace not working as expected

分類Dev

string formatting not working as expected

分類Dev

PHP Dateformat not working as expected

分類Dev

string.slice() not working as expected

分類Dev

Android String.replace not working

分類Dev

String replace in PHP with return function

分類Dev

Replace part of a string with Regex in PHP

分類Dev

How to replace string and upload with php

分類Dev

Android - Php Echo string not working

分類Dev

C++ "noskipws" is not working as expected, How to properly allow whitespace in string?

分類Dev

PHP preg_replace string path given

分類Dev

PHP preg_replace string within variables

分類Dev

check and replace double expression in string PHP

分類Dev

PHP string replace to prepare PDO SQL

分類Dev

how to replace number in a string that contains brackets in php

分類Dev

RegEx Replace at end of string using php

分類Dev

rapply "replace" not behaving as expected

分類Dev

Replace a string in a text file using a shell script not working properly

分類Dev

String.replace() isn't working like I expect

分類Dev

AJAX call is not working as expected

分類Dev

jQuery .when().then() not working as expected

分類Dev

.vimrc file not working as expected

分類Dev

*ngIf not working as expected with observable

分類Dev

ItemIsAutoTristate flag not working as expected

分類Dev

KeyboardAvoidingView not working as expected on IOS

分類Dev

PowerShell variables not working as expected

分類Dev

XPath logical 'and' not working as expected?

分類Dev

CancellationTokenSource not working as expected on a TaskCompletionSource

分類Dev

Why is .on() Not working as expected in jquery?

Related 関連記事

ホットタグ

アーカイブ