Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fixing unclosed HTML tags

Tags:

html

dom

php

I am working on some blog layout and I need to create an abstract of each post (say 15 of the lastest) to show on the homepage. Now the content I use is already formatted in html tags by the textile library. Now if I use substr to get 1st 500 chars of the post, the main problem that I face is how to close the unclosed tags.

e.g

<div>.......................</div>
<div>...........
     <p>............</p>
     <p>...........| 500 chars
     </p>
<div>  

What I get is two unclosed tags <p> and <div> , p wont create much trouble , but div just messes with the whole page layout. So any suggestion how to track the opening tags and close them manually or something?

like image 822
satin Avatar asked Dec 14 '11 06:12

satin


People also ask

What is unclosed tag in HTML?

What Is a Void Element? The void elements or singleton tags in HTML don't require a closing tag to be valid. These elements are usually ones that either stand alone on the page ​or where the end of their contents is obvious from the context of the page itself.

How do I close an unclosed tag in HTML?

XHTML is more strict than HTML, and requires that all open tags must be closed, even if they're not container tags. Therefore, non-container tags end in />. For example, the tag for a line break is <br />.

How do you check tags in HTML?

All major browsers have an option that enables you to see the page's source code when you right-click. Click “View Page Source” in Firefox and Chrome, for example. Search for additional meta tags on the page and note the attributes and values that appear next to those.

What happens when HTML tag is not closed?

If you don't add a closing tag the browser won't know where it ends. It will take the next tag and think it belongs to the previous tag without the closing tag.


2 Answers

There are lots of methods that can be used:

  1. Use a proper HTML parser, like DOMDocument
  2. Use PHP Tidy to repair the un-closed tag
  3. Some would suggest HTML Purifier
like image 133
ajreal Avatar answered Oct 07 '22 19:10

ajreal


As ajreal said, DOMDocument is a solution.

Example :

$str = "
<html>
 <head>
  <title>test</title>
 </head>
 <body>
  <p>error</i>
 </body>
</html>
";

$doc = new DOMDocument();
@$doc->loadHTML($str);
echo $doc->saveHTML();

Advantage : natively included in PHP, contrary to PHP Tidy.

like image 17
Jerry Avatar answered Oct 07 '22 20:10

Jerry