应该如何用PHP(简单html dom解析器)背景图像和网页的其他图像进行解析? [英] How should parse with PHP (simple html dom parser) background images and other images of webpage?

查看:77
本文介绍了应该如何用PHP(简单html dom解析器)背景图像和网页的其他图像进行解析?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何与PHP(简单html dom/etc.)背景和网页的其他图像一起解析?

情况1:内联CSS

<div id="id100" style="background:url(/mycar1.jpg)"></div>

情况2:html页面内的css

<div id="id100"></div>

<style type="text/css">
#id100{
background:url(/mycar1.jpg);
}
</style>

情况3:单独的CSS文件

<div id="id100" style="background:url(/mycar1.jpg);"></div>

external.css

#id100{
background:url(/mycar1.jpg);
}

案例4:img标签中的图片

案例4的解决方法,因为他出现在 php简单html dom解析器中:

// Create DOM from URL or file
$html = file_get_html('http://www.google.com/');

// Find all images
foreach($html->find('img') as $element)
       echo $element->src . '<br>';

请帮助我解析案例1,2,3.

如果有更多案例,请写下来,如果可以的话请单独提及.

If exist more cases please write them, with soltion if you can please.

谢谢

推荐答案

对于情况1:

// Create DOM from URL or file 
$html = file_get_html('http://www.google.com/');

// Get the style attribute for the item
$style = $html->getElementById("id100")->getAttribute('style');

// $style = background:url(/mycar1.jpg)
// You would now need to put it into a css parser or do some regular expression magic to get the values you need.

对于案例2/3:

// Create DOM from URL or file
$html = file_get_html('http://www.google.com/');

// Get the Style element
$style = $html->find('head',0)->find('style');

// $style now contains an array of style elements within the head. You will need to work out using attribute selectors what whether an element has a src attribute, if it does download the external css file and parse (using a css parser), if it doesnt then pass the innertext to the css parser.

这篇关于应该如何用PHP(简单html dom解析器)背景图像和网页的其他图像进行解析?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆