简体   繁体   中英

Replace <br style=“…”> with <br>

I'm looking for a way or a regular expression to change a break tag like:

<br style="font-size:12.8000001907349px">

to:

<br>

Right now I'm using this, but doesn't work as expected.

preg_replace("/<br\W*?\/>/", "<br>", $the_string);

How can I change my code to get it to work?

Just keep it simple, like this:

preg_replace("/<br.*?>/", "<br>", $the_string);

Basically .*? means:

  • .*? matches any character (except newline)
    • Quantifier: *? Between zero and unlimited times, as few times as possible, expanding as needed [lazy]

Change the <br> tag into a <br /> tag.

This will work:

preg_replace("/<br.*?\/>/", "<br />", $the_string);

Take this Regex expression for strings like:

Search this: <br anykindoftext>

preg_replace("/<br\s+.*>/", "<br>", $the_string);

When you search br tag with style you need:

Search this: <br style=“…”>
preg_replace("/<\s*br\s+style\s*=\s*".*"\s*>/", "<br>", $the_string);

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