Call to undefined method DOMDocument::getElementsByClassName()(调用未定义的方法 DOMDocument::getElementsByClassName())
问题描述
我正在尝试从以下 img 标签中获取 alt 标签...
<a href="/Electronic-Deals/b/ref=amb_link_185249707_2?ie=UTF8&node=4192584031&pf_rd_m=A1VBAL9TL5WCBF&pf_rd_s=center-amp_rd_s=center-amp_amp_pZ12&node=4192584031&pf_rd_s=center-amp_amp;JfZM;pf_rd_t=701&pf_rd_p=736512207&pf_rd_i=20"><img src="http://g-ecx.images-amazon.com/images/G/31/rcx-events/cat-navs-electronics1._V335880105_.png" alt="Electronics" border="0"高度=140"宽度=170"></a>
为此,我尝试了以下代码...
$dom = new DOMDocument();@$dom->loadHTML($html2);foreach($dom->getElementsByClassName("localImage") as $tr) {$name = '';foreach($tr->getElementsByTagName('img') as $i){$name = $i->getAttribute('alt');}回声 $name;
但是我收到以下错误...
调用未定义的方法 DOMDocument::getElementsByClassName()
任何人都可以帮助我哪里出错了...因为我之前尝试过这种代码模式,但之前从未遇到过这样的问题.
Class DOMDocument 不包含方法 getElementsByClassName
使用 xpath
$xpath = new DOMXpath($dom);$xpath->query('//div[contains(@class, "localImage")]');//DOMNodeList 实例
I am trying to fetch the alt tag from the following img tag...
<div class="localImage">
<a href="/Electronic-Deals/b/ref=amb_link_185249707_2?ie=UTF8&node=4192584031&pf_rd_m=A1VBAL9TL5WCBF&pf_rd_s=center-new-12&pf_rd_r=07C4YQ4KZ15MZJQBT2PD&pf_rd_t=701&pf_rd_p=736512207&pf_rd_i=20">
<img src="http://g-ecx.images-amazon.com/images/G/31/rcx-events/cat-navs-electronics1._V335880105_.png" alt="Electronics" border="0" height="140" width="170"></a>
</div>
For this I have tried the following code...
$dom = new DOMDocument();
@$dom->loadHTML($html2);
foreach($dom->getElementsByClassName("localImage") as $tr) {
$name = '';
foreach($tr->getElementsByTagName('img') as $i)
{
$name = $i->getAttribute('alt');
}
echo $name;
But am getting the following error...
Call to undefined method DOMDocument::getElementsByClassName()
Can anyone please help me where am getting it wrong...as I have tried this code pattern earlier but never faced such issue earlier.
Class DOMDocument does not contains method getElementsByClassName
Use xpath
$xpath = new DOMXpath($dom);
$xpath->query('//div[contains(@class, "localImage")]'); //instance of DOMNodeList
这篇关于调用未定义的方法 DOMDocument::getElementsByClassName()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:调用未定义的方法 DOMDocument::getElementsByClassName(
基础教程推荐
- HTTP 与 FTP 上传 2021-01-01
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- 使用 PDO 转义列名 2021-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- PHP 守护进程/worker 环境 2022-01-01
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01