I try to load some html to DOMDocument php object. But it cannot load an anchor element that has the attribute href with ampersand in it. This is the html code to load:
<p>Jaźń</p>
<p>gęślą<a href="http://example.com/?abc&def">zażółć</a></p>
<p>Hello!</p>
As you can see there is ampersand between abc and def in query string of url. It is utf-8 text.
Loading that to DOMDocument causes warning unless ampersand is removed. The warning is
Warning: DOMDocument::loadHTML(): htmlParseEntityRef: expecting ';' in Entity, line: 2 in /in/MXN5X on line 8
In my phpunit it causes an error.
The code is this:
<?php
$html = <<< 'WOW'
<p>Jaźń</p>
<p>gęślą<a href="http://example.com/?abc&def">zażółć</a></p>
<p>Hello!</p>
WOW;
$dom = new DOMDocument();
$dom->loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', "UTF-8"), LIBXML_HTML_NODEFDTD);
Online version: https://3v4l.org/MXN5X
How to load the anchor element with ampersand to DOMDocument object?
I had the same problem. you should use & instead of & and it runs good...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With