Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacing text between two limts

Tags:

regex

php

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
like image 558
kabuto178 Avatar asked Feb 12 '13 12:02

kabuto178


1 Answers

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']);
like image 184
m4t1t0 Avatar answered Sep 24 '22 03:09

m4t1t0