Skip to content
Advertisement

Replacing text between two limts

I have been trying to get the text between two symbols to be replaced with preg_replace, but alas still not quite getting it right as I get a null output that is empty string, this is what I have so far

$start = '["';
$end   = '"]';
$msg   = preg_replace('#('.$start.')(.*)('.$end.')#si', '$1 test $3', $row['body']);

So an example output I am looking for would be:

normal text [everythingheregone] after text 

To

 normal text [test] after text

Advertisement

Answer

You are defining $start and $end as arrays, but using it as normal variables. Try changing your code to this:

$start = '[';
$end  = ']';
$msg = preg_replace('#('.$start.')(.*)('.$end.')#si', '$1 test $3', $row['body']);

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement