gpt4 book ai didi

php - DOMNodeList::getAttribute 未定义

转载 作者:行者123 更新时间:2023-11-28 02:38:20 25 4
gpt4 key购买 nike

我尝试运行此代码,但 php 抛出: fatal error :调用路径/to/feature-manager.php 中未定义的方法 DOMNodeList::getAttribute()

这个文件包含html

//features.php

<div class="col-md-4">
<div class="feature">
<img class="img-fluid" src="media/artwork_icon_512.png" id="feature-icon1">
<h4>Feature 1</h4>
</div>
</div><!---->


<div class="col-md-4">
<div class="feature">
<img class="img-fluid" src="media/dress_flat_icon-512.png" id="feature-icon2">
<h4>Feature 2</h4>
</div>
</div><!---->


<div class="col-md-4">
<div class="feature">
<img class="img-fluid" src="media/users-512.png" id="feature-icon3">
<h4>Feature 3</h4>
</div>
</div>

然后我调用它到这个文件

//feature-manager.php

$read=file_get_contents('../include/features.php');
$features = explode('<!---->', $read);

$doc= new DOMDocument();
foreach ($features as $feature) {

$doc->loadHTML($feature);
$img = $doc->getElementsByTagName("img");
$imgsrc = $img->getAttribute('src');

echo $imgsrc;
显然我想从中获取 src attrib 但不知何故无法识别 getAttribute()

最佳答案

你必须遍历 getElementsByTagName() 的结果

该函数返回一个DomNodeList,getAttribute属于DOMElement

//feature-manager.php

$read=file_get_contents('../include/features.php');
$features = explode('<!---->', $read);

$doc= new DOMDocument();
foreach ($features as $feature) {

$doc->loadHTML($feature);
$images = $doc->getElementsByTagName("img");
foreach($images as $img){

$imgsrc = $img->getAttribute('src');
echo $imgsrc;
}
}

关于php - DOMNodeList::getAttribute 未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45773079/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com