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>

TartanLlama
  • 63,752
  • 13
  • 157
  • 193
ali
  • 61
  • 1
  • 1
  • 2

2 Answers2

21

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

joash
  • 2,205
  • 2
  • 26
  • 31
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.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • 2
    To disable the warning, use `libxml_use_internal_errors(true);`, more here https://stackoverflow.com/a/11819635/1066234 – Avatar Mar 26 '18 at 09:31