Unexpected end tag p in Entity in DOMDocument::loadHTML() error
Asked Answered
W

2

6

Why do I get warning for this code?

$content ='<p>
 <a href="http://www.we.com/1000">text </a>
 text 
 <a href="http://www.we.com/2345">text </a>
  text 
</p>

<p>text</p>

<p>
  <table border="1" cellpadding="0" cellspacing="0" dir="rtl"> 
      <tbody> 
          <tr> 
              <td>text </td> 
              <td>text </td> 
              <td>text </td> 
          </tr> 
          <tr> 
              <td>text </td> 
              <td>text </td> 
              <td>text </td> 
          </tr> 
      </tbody> 
  </table>
</p>';

$doc = new DOMDocument('1.0', 'UTF-8');
$doc->loadHTML($content);

The warning is:

Warning: DOMDocument::loadHTML(): Unexpected end tag : p in Entity, line: 25 in /home/admin/domains/we.com/public_html/refresh/lib/core.php on line 2213 <p> <a href="http://www.we.com/1000">text </a> text <a href="http://www.we.com/2345">text </a> text </p> <p>text</p> <p> </p><table border="1" cellpadding="0" cellspacing="0" dir="rtl"><tbody><tr><td>text </td> <td>text </td> <td>text </td> </tr><tr><td>text </td> <td>text </td> <td>text </td> </tr></tbody></table>

Willtrude answered 3/3, 2015 at 12:51 Comment(1)
Improve code formatting, grammar, escape html.Ean
I
22

Add @ to suppress the warning as follows

@$doc->loadHTML($content);

This is because most of HTML objects are not perfectly formatted or even elements like "p" are automatically closed

Inflectional answered 4/11, 2020 at 14:45 Comment(1)
Great thanks, I took the @ out from a snippet I got elsewhere thinking it was a typo! lolFestschrift
D
6

The end tag for paragraphs is optional. A table may not appear within a paragraph. The table start tag implicitly ends that paragraph. The next paragraph end tag has no open paragraph to close.

See "Tag omission in text/html" in the spec for p.

Dimond answered 6/3, 2015 at 8:48 Comment(1)
To disable the warning, use libxml_use_internal_errors(true);, more here https://mcmap.net/q/433224/-domdocument-loadhtml-doesn-39-t-work-properly-on-a-serverEisteddfod

© 2022 - 2025 — McMap. All rights reserved.