Page 1 of 1
preg_replace line break with space
Posted: Wed Nov 08, 2006 6:38 am
by SmokyBarnable
I have a string that has <br /> 's in it. I want to remove these and replace with just a space. However when I try to use preg_replace it changes them all to <> instead of just a space. Here is the code I'm using.
Code: Select all
$msg = preg_replace('<br />', ' ' , $msg);
Thanks for any help.
Posted: Wed Nov 08, 2006 9:19 am
by theYinYeti
Regular expressions must be surrounded by a delimiter. The most common delimiter is '/', but in your case you have a '/' inside the RE, so another character would be better; I often use '#':
Code: Select all
$msg = preg_replace('#<br />#', ' ' , $msg);
Apart from that, were I you, I'd rather write:
Code: Select all
$msg = preg_replace('#<br\s*/?>#', ' ' , $msg);
Yves.
Re: preg_replace line break with space
Posted: Wed Nov 08, 2006 9:31 am
by jmut
SmokyBarnable wrote:I have a string that has <br /> 's in it. I want to remove these and replace with just a space. However when I try to use preg_replace it changes them all to <> instead of just a space. Here is the code I'm using.
Code: Select all
$msg = preg_replace('<br />', ' ' , $msg);
Thanks for any help.
Code: Select all
$string = preg_quote('<br />');
$msg = preg_replace("#$string#", ' ' , $msg);
Posted: Wed Nov 08, 2006 9:44 am
by printf
I would think, you only need a string function for this...
Code: Select all
$msg = str_replace ( '<br />', ' ', $msg );
printf
Posted: Wed Nov 08, 2006 9:50 am
by jmut
printf wrote:I would think, you only need a string function for this...
Code: Select all
$msg = str_replace ( '<br />', ' ', $msg );
printf
so true

I just wanted to point out how preg_quote is useful in such cases.
Posted: Wed Nov 08, 2006 11:03 am
by SmokyBarnable
Thanks....all those replies were very helpful.
php is cool!
