Regular PHP expression, how to remove open html tags?

Asked

Viewed 573 times

2

Hi! I need to remove an html chunk that bugged my string, it looks like this:

e="text-align:left;"> Texto normal ...

This generates a visual error. Another error generating is at the end of the text ex:

<p style="text-ali ...

Here was missing the rest of the tag and closing, which generates more errors.

Have some regular expression to remove only (>) and (<) when you are under formatting error, in case an attempt fails html markup?

1 answer

1

According to this Stack Overflow Response in English, you can do so:

$html = preg_replace("/<([^<>]*)(?=<|$)/", "$1", $html); # remove '<' os não fechados
$html = preg_replace("/(^|(?<=>))([^<>]*)>/", "$1", $html); # remove os '>' não fechados

Still according to the second reply of the link above, we have a following explanation:

Half-assed translation of mine:

To a < unopened, it is possible to replace <(?=[^>]*(<|$)) by an empty string. It matches all < which are not followed by a > before the next <, or at the end of the line.

To the > unopened, you can replace ((^|>)[^<]*)> for $1. It matches the text that begins with a > (or line start), does not contain < and ends with the >. $1 represents everything except the last >.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.