从php中的数据中删除损坏的html标签

I am working on php app , where i got random text from different source like (email,db ect).Now I am facing a problem .I have text that contain broken HTML element like

$purl  ='FTP details are as 
follow:User name : Mahmud
div>password :1234556Than
ks ';

.I tried strip_tags and some preg matching algo's but didn't work.How can i remove the HTML elements if its not complete like in above div> tag.I know this type question asked earlier but i didn't know how can i do it.Thanks for any help.

For further details i am adding this Link.I am fetching emails and then getting specific portion of emails using DOM.

How about I'm an user and I want my username to be <span man?

You can't actually know when the text should be "corrected" because its a broken tag or when it's not.

You should just do something on your input. Are you getting this text from a curl output? But anyways, as I said, just check your reading input.

You need HTML TIDY installed and configured in your php for details on this refer to this link

php.net/manual/en/book.tidy.php

And this question has been asked earlier, refer to this link for code (answer)

Remove HTML Entity if Incomplete

http://php.net/manual/en/tidy.parsestring.php



<?php
ob_start();
?>

<html>
    <head>
        <title>test</title>
    </head>
    <body>
        <p>error<br>another line</i>
    </body>
</html>

<?php

$buffer = ob_get_clean();
$config = array('indent' => TRUE,
        'output-xhtml' => TRUE,
        'wrap' => 200);

$tidy = tidy_parse_string($buffer, $config, 'UTF8');

$tidy->cleanRepair();
echo $tidy;

?>