"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Suppress \"htmlParseEntityRef: expecting \';\' in Entity\" Warning in PHP?

How to Suppress \"htmlParseEntityRef: expecting \';\' in Entity\" Warning in PHP?

Published on 2024-11-08
Browse:549

How to Suppress \

Resolving "htmlParseEntityRef: expecting ';' in Entity" Warning

When loading HTML content into a DOMDocument, you may encounter the warning "htmlParseEntityRef: expecting ';' in Entity." This error often arises due to malformed HTML entities in the loaded content. To alleviate this warning while ensuring proper entity resolution, follow these steps:

  1. Enable Internal Errors: Utilizing the libxml_use_internal_errors(true) function allows internal XML parsing errors to be recorded without abruptly terminating your script. This enables you to handle and process the errors gracefully.
  2. Load HTML: Subsequent to activating internal errors, load the HTML content into the DOMDocument as usual using $dom->loadHTML($html).
  3. Disable Internal Errors: After loading the HTML, disable internal errors by invoking libxml_use_internal_errors($internalErrors) with the previously stored error level to revert to the default error handling behavior.

By employing this technique, the warning will be suppressed, and the DOMDocument will be correctly populated with the loaded HTML content.

// create new DOMDocument
$document = new \DOMDocument('1.0', 'UTF-8');

// set error level
$internalErrors = libxml_use_internal_errors(true);

// load HTML
$document->loadHTML($html);

// Restore error level
libxml_use_internal_errors($internalErrors);
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3