We need a ready-made script (library) to close the html tags correctly.

For example:

<div> <p><p> </div> 

should not close like this:

  <div> <p><p> </div></p></p> 

Or vice versa, if there is a closed tag and there is no open tag, you need to set the opening tag correctly.

Advise something please.

Closed due to the fact that it is necessary to reformulate the question so that you can give an objectively correct answer to the party Nicolas Chabanovsky 16 Apr '16 at 8:01 .

The question gives rise to endless debates and discussions based not on knowledge, but on opinions. To get an answer, rephrase your question so that it can be given an unambiguously correct answer, or delete the question altogether. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • Tags must be closed in the same way as brackets. You cannot close the bracket like this: ( [ { ] } ) . - MichaelPak
  • What you are writing about is an AI level task. Maybe tell me why it is for you - suddenly you are trying to hammer nails with a microscope. - newman
  • Wrote the same, need a script. not a browser, validator or text editor. I do not do this for myself, there are 100k descriptions of products that use html. The correct html is not everywhere and the layout of the product card is floating because of this. - aat
  • 2
    @aat, accept, there is no such library / script. If when the browser closes tags, the layout goes, then when the tags are closed by libraries that use the same engines as browsers, everything will also go. - Grundy
  • [1] The parameter auto_close_tags appeared in Sublime Text 3, it helps a lot with closing tags and detecting unclosed ones. // [2] I use the HTMLPrettify plugin that formats markup into readable. Unnecessary or unclosed tags become visible on the surface. // [3] I send links to the finished pages of my site depending on the content through the HTML validator or CSS validator . Thank. - Sasha Chernykh

6 answers 6

some part of the problem can be solved by loading into a domdocument and unloading it back. He is quite loyal to such errors.

For example, such code

 $dom = new DomDocument(); $dom->loadHTML('<div> <p><p> </div>'); echo $dom->saveHTML(); 

will give the following result:

 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"> <html><body><div> <p></p><p> </p></div></body></html> 
  • Needless to say that the input markup was valid? - Qwertiy
  • there are two unclosed paragraphs - an example from the question - splash58
  • Two unclosed paragraphs are valid markup. - Qwertiy
  • the question asks how to close an unclosed element. I wrote a version of how. What is your question about? - splash58

Tidy should go

Tidy is an interface to the Tidy HTML cleanup and repair utility, which, in addition to cleaning and restoring, also allows you to manipulate HTML documents as well as navigate the document tree.

    In order to keep track of nesting and closing tags, use the HTML code validator and text editor for manual tag correction. I recommend the validator on the first link of the list.

    • W3C validator ( link )
    • For Firefox ( link )
    • For Chrome ( link )
    • one
      validators like just checking and not placing - Grundy
    • Yes. Using a validator and a text editor with syntax highlighting (for example, Notepad ++ ) makes writing and editing HTML code much easier. - Max
    • 2
      but does not automate the placement of paired tags :) - Grundy
    • @Grundy not only automates everything. Few people can do it .... Especially since the author wants it - Alexey Shimansky
    • @ Alexey Shimansky, he himself said - browsers do an excellent job :) - Grundy

    Absolutely correct tags can be closed only manually.

    You can put every block in the html-markup in <table><tr><td> . Not very nice, but inside the table not closed tags will not break the rest of your layout. The browser normally understands them. Here on SO it seems done.

    Although there is such a moment that if your text has unclosed <table> tags, then there is a chance that the layout will break.

      What about the phpQuery library?

      https://habrahabr.ru/post/69149/

        Cut out all the tags before displaying the title description, etc. using strip_tags() and do not suffer.