繁体   English   中英

来自网址的PHP Preg_match图片

[英]PHP Preg_match Image from url

我正在尝试解析一个网站并获取图像的名称或网址。

示例网址: http : //www.theworkingmanstore.com/georgia-gr14-infants-romeo.aspx

一个<td>有6张图像或更多,我只想在该<td>获得第一个img src。

我确信可以使用Dom Parser完成此操作,但是我没有经验。

任何援助将不胜感激。

谢谢

$html = file_get_contents($url);
$reg = '/img src=["\']?([^"\' ]*)["\' ]/';
preg_match_all($reg, $html, $m);
$arr = array_map(function($v){
return trim(str_replace(array('img src=', 'http://www.theworkingmanstore.com'), '', $v), '"');}, $m[0]);
print_r($arr)

输出:这是正则表达式的输出

Array
(
    [0] => /images/logo2.png
    [1] => /images/mod_head_category_lt.gif
    [2] => '/images/products/display/GR14_EXTRALARGE.jpg'
    [3] => '/images/products/thumb/GR14_EXTRALARGE.jpg'
    [4] => '/images/products/thumb/GR14_8_EXTRALARGE.jpg'
    [5] => '/images/products/thumb/GR14_5_EXTRALARGE.jpg'
    [6] => '/images/products/thumb/GR14_3_EXTRALARGE.jpg'
    [7] => '/images/products/thumb/GR14_42_EXTRALARGE.jpg'
    [8] => '/images/products/thumb/GR14_2_EXTRALARGE.jpg'
    [9] => /images/freeshipping.jpg
    [10] => /images/facebook_32.png
    [11] => images/twitter_32.png
    [12] => images/googleplus_32.png
    [13] => images/pinterest_32.png
    [14] => /images/payments.gif
    [15] => /images/brands/the-working-man.jpg
)

尝试了Dom Parser的建议:

$html = file_get_contents($url) ;
$dom = new DOMDocument();
$dom->loadHtml($html);    
$xpath = new DOMXPath($dom);
echo $xpath->evaluate(
'string(//td/a[@id = "Zoomer"]/descendant::img[1]/@src)'
);

输出错误:警告:DOMDocument :: loadHTML()[domdocument.loadhtml]:实体中的标签导航无效

在DOM中,任何东西都是节点, img元素和src属性。 XPath允许您从DOM中获取节点列表。

$dom = new DOMDocument();
$dom->loadHtml($html);
$xpath = new DOMXPath($dom);
foreach ($xpath->evaluate('//img/@src') as $src) {
  echo $src->value, "\n";
}

输出:

http://www.theworkingmanstore.com/images/products/display/GR14_EXTRALARGE.jpg
http://www.theworkingmanstore.com/images/products/detail/GR14_EXTRALARGE.jpg
/images/products/thumb/GR14_EXTRALARGE.jpg
/images/products/thumb/GR14_8_EXTRALARGE.jpg
/images/products/thumb/GR14_5_EXTRALARGE.jpg
/images/products/thumb/GR14_3_EXTRALARGE.jpg
/images/products/thumb/GR14_42_EXTRALARGE.jpg
/images/products/thumb/GR14_2_EXTRALARGE.jpg

XPath允许退出复杂条件。 以下示例在任何td内输出第一个imgsrc属性。

$dom = new DOMDocument();
$dom->loadHtml($html);    
$xpath = new DOMXPath($dom);

foreach ($xpath->evaluate('//td/descendant::img[1]/@src') as $src) {
  echo $src->value, "\n";
}

输出:

http://www.theworkingmanstore.com/images/products/display/GR14_EXTRALARGE.jpg

问题中的HTML仅包含一个td ,更重要的是img位于具有id属性a元素内。 因此,它必须是一个唯一的值。 这允许它直接在XPath中强制转换节点列表,并将其作为字符串返回。

$dom = new DOMDocument();
$dom->loadHtml($html);    
$xpath = new DOMXPath($dom);
echo $xpath->evaluate(
  'string(//td/a[@id = "Zoomer"]/descendant::img[1]/@src)'
);

输出:

http://www.theworkingmanstore.com/images/products/display/GR14_EXTRALARGE.jpg

您可以尝试使用此正则表达式。

$html = 'Your HTML';
$reg = '/img src=["\']?([^"\' ]*)["\' ]/';
preg_match_all($reg, $html, $m);
$arr = array_map(function($v){
    return trim(str_replace(array('img src=', 'http://www.theworkingmanstore.com'), '', $v), '"');
}, $m[0]);

print '<pre>';
print_r($arr);
print '</pre>';

输出:

Array
(
    [0] => /images/products/display/GR14_EXTRALARGE.jpg
    [1] => /images/products/detail/GR14_EXTRALARGE.jpg
    [2] => /images/products/thumb/GR14_EXTRALARGE.jpg
    [3] => /images/products/thumb/GR14_8_EXTRALARGE.jpg
    [4] => /images/products/thumb/GR14_5_EXTRALARGE.jpg
    [5] => /images/products/thumb/GR14_3_EXTRALARGE.jpg
    [6] => /images/products/thumb/GR14_42_EXTRALARGE.jpg
    [7] => /images/products/thumb/GR14_2_EXTRALARGE.jpg
)

暂无
暂无

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

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