PHP Replace entire sentence from string

473 Views Asked by At

str_replace only let's me replace words.

The sentence i'm searching for is:

[TD="align: left"]".$num."[/TD] [TD="align: left"][/TD]

And I need to replace it with:

[TD="align: left"]".$num."[/TD] [TD="align: left"]".$uid"[/TD]

The thing im searching is in a variable called $newres

I've tried str_replace but as said above it only lets me replace a word at a time, I looked into making them an array but that won't work because there are multiple

[TD="align: left"][/TD]

in the string

Thanks!

1

There are 1 best solutions below

3
On

try this

$q='[TD="align: left"]3[/TD] [TD="align: left"][/TD]';
$q .='[TD="align: left"]33[/TD] [TD="align: left"][/TD]';
$num=3;
$uid=6;
$w=str_replace('[TD="align: left"]'.$num.'[/TD] [TD="align: left"][/TD]','[TD="align: left"]'.$num.'[/TD] [TD="align: left"]'.$uid.'[/TD]',$q);

var_dump($q,$w);

output:

string '[TD="align: left"]3[/TD] [TD="align: left"][/TD][TD="align: left"]33[/TD] [TD="align: left"][/TD]' (length=97)

string '[TD="align: left"]3[/TD] [TD="align: left"]6[/TD][TD="align: left"]33[/TD] [TD="align: left"][/TD]' (length=98)