简体   繁体   中英

How to make this regular expression?

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!

Replace |<br\\s*/?>|i with "a string".

(Note that you can't parse HTML with regex even just for <br /> — eg 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.)

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

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM