Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7998419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T15:12:31+00:00 2026-06-04T15:12:31+00:00

I’m attempting to parse HTML code with DOMDocument, do stuff like changes to it,

  • 0

I’m attempting to parse HTML code with DOMDocument, do stuff like changes to it, then assemble it back to a string which I send to the output.

But there a few issues regarding parsing, meaning that what I send to DOMDocument does not always come back in the same form 🙂

Here’s a list:

  1. using ->loadHTML:

    • formats my document regardless of the preserveWhitespace and formatOutput settings (loosing whitespaces on preformatted text)
    • gives me errors when I have html5 tags like <header>, <footer> etc. But they can be supressed, so I can live with this.
    • produces inconsistent markup – for example if I add a <link ... /> element (with a self-closing tag), after parsing/saveHTML the output will be <link .. >
  2. using ->loadXML:

    • encodes entities like > from <style> or <script> tags: body > div becomes body &gt; div
    • all tags are closed the same way, for example <meta ... /> becomes <meta...></meta>; but this can be fixed with an regex.

I didn’t try HTML5lib but I’d prefer DOMDocument instead of a custom parser for performance reasons


Update:

So like the Honeymonster mentioned using CDATA fixes the main problem with loadXML.

Is there any way I could prevent self closing of all empty HTML tags besides a certain set, without using regex?

Right now I have:

$html = $dom->saveXML($node);

$html = preg_replace_callback('#<(\w+)([^>]*)\s*/>#s', function($matches){

       // ignore only these tags
       $xhtml_tags = array('br', 'hr', 'input', 'frame', 'img', 'area', 'link', 'col', 'base', 'basefont', 'param' ,'meta');

       // if a element that is not in the above list is empty,
       // it should close like   `<element></element>` (for eg. empty `<title>`)
       return in_array($matches[1], $xhtml_tags) ? "<{$matches[1]}{$matches[2]} />" : "<{$matches[1]}{$matches[2]}></{$matches[1]}>";
}, $html);

which works but it will also do the replacements in the CDATA content, which I don’t want…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T15:12:32+00:00Added an answer on June 4, 2026 at 3:12 pm

    Unfortunately, or possibly fortunately, domdocument is designed to not try to preserve formatting from the original document. This is to make the parser’s internal state easier to manage by keeping all elements the same style. Afaik most parsers will create a tree representation in memory and not worry about the textual formatting until the user requests such. This is why your self closed tags are output with separate closing tags. The good news is that it doesn’t matter.

    As to style tags and script tags getting <> converted to &lt;&gt;, you may be able to avoid the conversion by surrounding the contents of the element in question with the recommended cdata tags thusly:

    <style>
      /*<![CDATA[*/
        body > div {
          width: 50%;
        }
      /*]]>*/
    </style>
    

    The comment /* */ around the cdata declarations are to allow for broken clients which don’t know about cdata sections and instead treat the declarations as CSS code. If you’re using the document internally only then you may omit the /* */ comment surrounds and have the cdata declaration only. You may encounter issues with the aforementioned broken clients if you manipulate the document and then send it to the browser without checking to ensure the /* */ comments are retained; I am unsure whether domdocument will retain these or not.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I have this code to decode numeric html entities to the UTF8 equivalent character.
I would like to run a str_replace or preg_replace which looks for certain words
i got an object with contents of html markup in it, for example: string
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.