Regular PHP expression, how to remove open html tags?

2

Hello! I need to remove from my string an html snippet that bugged, it goes like this:

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

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

<p style="text-ali ...

The rest of the tag and closing were missing here, which causes more errors.

Do you have any regular expressions to remove only the (>) and (<) when they are formatted, in the case of an attempt to html markup?

    
asked by anonymous 16.05.2016 / 15:44

1 answer

1

According to this answer in Stack Overflow , you can do this: / p>

$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 response of the link above, we have a following explanation:

Half-mouth translation of my:

  

For a < not closed, you can replace <(?=[^>]*(<|$)) with an empty string. It matches all < that are not followed by a > closure before the next < , or at the end of the line.

     

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

    
16.05.2016 / 16:23