I want to replace all line break (<br>
or <br />
) in a string with PHP. What regular expression I should have to do this, using preg_replace?
Thanks!
I want to replace all line break (<br>
or <br />
) in a string with PHP. What regular expression I should have to do this, using preg_replace?
Thanks!
Edit:
$string = str_replace(array('<br>','<br />'),'', $string);
Oh, regex isn't ideal tool for everything
So if you decide that insisting on using preg_replace
isn't meaningful anymore, you can use this
Replace |<br\s*/?>|i
with "a string".
(Note that you can't parse HTML with regex even just for <br />
— e.g. what if this <br />
is part of a string in some inline Javascript? Use a parser to get reliable result.)
/<br[^>]*>/
Should do ... and handles <br clear="all">
too. (If this is not a requirement, use KennyTM's solution instead. In all cases you should read his disclaimer.)
check this if it helps please post back if here is any problem.
$str = 'This is a test string<br>.and another string <br>that will replace<br> with <br />';
$pattern = "/<br>/";
$replace = "<br />";
echo preg_replace($pattern, $replace, $str);