Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to close unclosed HTML Tags?

Tags:

javascript

php

Whenever we are fetching some user inputed content with some editing from the database or similar sources, we might retrieve the portion which only contains the opening tag but no closing.

This can hamper the website's current layout.

Is there a clientside or serverside way of fixing this?

like image 394
Starx Avatar asked Jun 17 '10 06:06

Starx


People also ask

How do I close HTML tags?

An opening tag begins a section of page content, and a closing tag ends it. For example, to markup a section of text as a paragraph, you would open the paragraph with an opening paragraph tag <p> and close it with a closing paragraph tag </p> (closing tags always proceed the element with a /).

How do I close a self-closing tag?

A self-closing tag is an element of HTML code that has evolved in the language. Typically, the self-closing tag makes use of a “/” character in order to effectively close out a beginning tag enclosed in sideways carets.

What are unclosed tags 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.

Does head need a closing tag?

head is required and it should be used just once. It should start immediately after the opening html tag and end directly before the opening body tag.


1 Answers

Found a great answer for this one:

Use PHP 5 and use the loadHTML() method of the DOMDocument object. This auto parses badly formed HTML and a subsequent call to saveXML() will output the valid HTML. The DOM functions can be found here:

http://www.php.net/dom

The usage of this:

$doc = new DOMDocument(); $doc->loadHTML($yourText); $yourText = $doc->saveHTML(); 
like image 194
KJS Avatar answered Oct 06 '22 10:10

KJS