php-如何使用loadhtml从a href获取img url
如何使用dom loadhtml从href url获取img url?我尝试使用$link->nodeValue获取img src url,但不起作用 示例url源:php-如何使用loadhtml从a href获取img url,php,Php,如何使用dom loadhtml从href url获取img url?我尝试使用$link->nodeValue获取img src url,但不起作用 示例url源: <a href="www.google.com"><img src="www.google.com/test.jpg" />Photo NodeValue</a> 我的php代码: // ---------------------------------------------------
<a href="www.google.com"><img src="www.google.com/test.jpg" />Photo NodeValue</a>
我的php代码:
// -------------------------------------------------------------------------
// ----------------------- Get URLs From Source
// -------------------------------------------------------------------------
function getVidesURL($url) {
$web_source = $this->getSource($url);
if($web_source != '') {
$Data = $this->Websites_Data[$this->getHost($url)];
preg_match($Data['Index_Preg_Match'], $web_source, $Videos_Page);
$Videos_Page = $Videos_Page[$Data['Index_Preg_Match_Num']];
if($Videos_Page != '') {
$dom = new DOMDocument;
@$dom->loadHTML($Videos_Page);
$links = $dom->getElementsByTagName('a');
foreach ($links as $link) {
$Video_Status = "";
$Video_Error = "";
$Video = array(
"URL" => $link->getAttribute('href'),
"Title" => $link->getAttribute('title'),
"MSG" => $link->nodeValue,
);
// Get Image URL Start
$dom = new DOMDocument;
@$dom->loadHTML($Video['MSG']);
$Video_Image = $dom->getElementsByTagName('img');
foreach ($Video_Image as $Image) {
$Video = array(
"IMG" => $link->getAttribute('src'),
);
}
$Videos_URLs .= $Video['IMG'] . '<br />';
}
// Get Image URL Stop
return $Videos_URLs;
}
}
}
//-------------------------------------------------------------------------
//--------------------------从源代码获取URL
// -------------------------------------------------------------------------
函数getVidesURL($url){
$web\u source=$this->getSource($url);
如果($web_源!=''){
$Data=$this->Websites\u Data[$this->getHost($url)];
preg_match($Data['Index_preg_match'],$web_source,$Videos_Page);
$Videos_Page=$Videos_Page[$Data['Index_Preg_Match_Num'];
如果($Videos\u Page!=''){
$dom=新的DOMDocument;
@$dom->loadHTML($Videos\u页面);
$links=$dom->getElementsByTagName('a');
foreach($links作为$link){
$Video_Status=“”;
$Video_Error=“”;
$Video=阵列(
“URL”=>$link->getAttribute('href'),
“Title”=>$link->getAttribute('Title'),
“MSG”=>$link->nodeValue,
);
//获取图像URL开始
$dom=新的DOMDocument;
@$dom->loadHTML($Video['MSG']);
$Video_Image=$dom->getElementsByTagName('img');
foreach($Video\u Image作为$Image){
$Video=阵列(
“IMG”=>$link->getAttribute('src'),
);
}
$Videos_url.=$Video['IMG'].
;
}
//获取图像URL停止
返回$Videos\u URL;
}
}
}
我的代码唯一的问题是我不知道如何从a href获取img url这里有一个小函数,可以从HTML输入中提取图像源:
<?php
echo PHP_EOL;
var_dump(getImgSrcFromHTML('<a href="www.google.com"><img src="www.google.com/test.jpg" />Photo NodeValue</a><div><img src="www.google.com/test2.jpg" /></div><table><tr><td><img src="www.google.com/test3.jpg" /></td></tr></table>'));
echo PHP_EOL;
function getImgSrcFromHTML($html){
$doc = new DOMDocument();
$doc->loadHTML($html);
$imagepPaths = array();
$imageTags = $doc->getElementsByTagName('img');
foreach ($imageTags as $tag) {
$imagePaths[] = $tag->getAttribute('src');
}
if(!empty($imagePaths)) {
return $imagePaths;
} else {
return false;
}
}