$str = " <html> <head> <title>test</title> </head> <body> <p>error</i> </body> </html> "; $doc = new DOMDocument(); @$doc->loadHTML($str); echo $doc->saveHTML();