admin管理员组文章数量:1201147
According to .queryselector.php Dom\ParentNode::querySelector is supported in PHP >= 8.4.0. Pursuant to that I should think the following code would work:
$html = '<p><a href="blahblah">blahblah</a></p>';
$doc = new DOMDocument();
libxml_use_internal_errors(true);
$doc->loadHTML($html);
libxml_clear_errors();
$test = $doc->querySelector('a');
echo $test->getAttribute('href');
However, when I run it on PHP 8.4.3 I get this error:
Fatal error: Uncaught Error: Call to undefined method DOMDocument::querySelector()
Here it is on 3v4l:
.4.3
According to https://www.php.net/manual/en/dom-parentnode.queryselector.php Dom\ParentNode::querySelector is supported in PHP >= 8.4.0. Pursuant to that I should think the following code would work:
$html = '<p><a href="blahblah">blahblah</a></p>';
$doc = new DOMDocument();
libxml_use_internal_errors(true);
$doc->loadHTML($html);
libxml_clear_errors();
$test = $doc->querySelector('a');
echo $test->getAttribute('href');
However, when I run it on PHP 8.4.3 I get this error:
Fatal error: Uncaught Error: Call to undefined method DOMDocument::querySelector()
Here it is on 3v4l.org:
https://3v4l.org/q9VDR#v8.4.3
Share Improve this question edited Jan 21 at 14:43 hakre 198k55 gold badges446 silver badges854 bronze badges Recognized by PHP Collective asked Jan 21 at 13:50 neubertneubert 16.8k26 gold badges133 silver badges247 bronze badges1 Answer
Reset to default 4With PHP 8.4 note you need to use a different document object for that:
DOM\HTMLDocument
/DOMDocument
You can upgrade your code now this way, it is only about the loading if you alias to the old class name:
use DOM\HTMLDocument as DOMDocument;
# **NEW** PHP 8.4 as ^ before ^
$html = '<p><a href="blahblah">blahblah</a></p>';
$doc = DOMDocument::createFromString($html, LIBXML_HTML_NOIMPLIED);
# no <html>...</html> wrap
$test = $doc->querySelector('a');
echo $test->getAttribute('href');
Here it is on 3v4l.org: https://3v4l.org/JAhV6#v8.4.3
本文标签: how to use DomDocument39s querySelector in PHP 843Stack Overflow
版权声明:本文标题:how to use DomDocument's querySelector in PHP 8.4.3? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738629917a2103683.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论