Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ampersand in DOMDocument in href attribute of an anchor element and UTF-8

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?

like image 407
trzczy Avatar asked Oct 27 '25 14:10

trzczy


1 Answers

I had the same problem. you should use &amp; instead of & and it runs good...

like image 54
Remco Van Der Velde Avatar answered Oct 29 '25 03:10

Remco Van Der Velde



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!