简体   繁体   English

锚点元素的href属性和UTF-8中的DOMDocument中的“&”号

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

I try to load some html to DOMDocument php object. 我尝试将一些html加载到DOMDocument php对象。 But it cannot load an anchor element that has the attribute href with ampersand in it. 但是,它无法加载其中具有href属性和符号的锚元素。 This is the html code to load: 这是要加载的html代码:

<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. 如您所见,URL查询字符串中的abcdef之间有一个&符。 It is utf-8 text. 它是utf-8文字。 Loading that to DOMDocument causes warning unless ampersand is removed. 除非删除与号,否则将其加载到DOMDocument会引起警告。 The warning is 警告是

Warning: DOMDocument::loadHTML(): htmlParseEntityRef: expecting ';' 警告:DOMDocument :: loadHTML():htmlParseEntityRef:期待';' in Entity, line: 2 in /in/MXN5X on line 8 在实体中,第8行在/ in / MXN5X中的第2行

In my phpunit it causes an error. 在我的phpunit中,它导致错误。

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 在线版本: https//3v4l.org/MXN5X

How to load the anchor element with ampersand to DOMDocument object? 如何将与符号的锚元素加载到DOMDocument对象?

I had the same problem. 我有同样的问题。 you should use &amp; 您应该使用&amp; instead of & and it runs good... 而不是&,并且运行良好...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM