2

PHP(simple html dom / etc ..)の背景やその他のWebページの画像をどのように解析する必要がありますか?

ケース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タグ内の画像

彼がphpsimplehtml dom parserに表示されるケース4の解決策

// 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の解析を手伝ってください。

より多くのケースが存在する場合は、可能であれば解決策を添えて、それらを書いてください。

ありがとう

4

2 に答える 2

3

ケース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.
于 2010-07-21T13:27:20.917 に答える
1

ページから抽出する<img>には、次のような方法を試すことができます。

$doc = new DOMDocument(); 
$doc->loadHTML("<html><body>Foo<br><img src=\"bar.jpg\" title=\"Foo bar\" alt=\"alt\"></body></html>"); 
$xml = simplexml_import_dom($doc);
$images = $xml->xpath('//img'); 
foreach ($images as $img) 
    echo $img['src'] . ' ' . $img['alt'] . ' ' . $img['title']; 

詳細については、DOMDocumentのドキュメントを参照してください。

于 2010-07-10T19:35:08.650 に答える