2010-07-10 2 views
1

어떻게 PHP (간단한 HTML DOM/등 ..) 배경 및 웹 페이지의 다른 이미지를 구문 분석해야합니까?PHP (간단한 html dom 파서) 배경 이미지 및 웹 페이지의 다른 이미지를 구문 분석하는 방법은 무엇입니까?

사례 1 : 인라인 CSS

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

케이스 2 : 별도의 CSS 파일

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

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

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

케이스 (3) 내부 CSS

// 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>'; 

는 제발 도와주세요 : 17,451,515,

external.css

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

경우 4 : 그가 php simple html dom parser에 나타나는 img 태그 내부 이미지

솔루션은 4를 구분하기 구문 분석 1,2,3.

더 많은 경우 존재하는 경우 soltion으로 작성하여주십시오. 자세한 내용은 DOMDocument에 대한

$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']; 

참조 문서 : 당신이 뭔가를 시도 할 수있는 페이지에서 <img>를 추출하려면

감사

+0

는 (오늘 포함)하기 전에 여러 번 대답하고있다. 외부 CSS 파일은 SGML/XML 라이브러리에서 처리 할 수 ​​없습니다. 또한 노드 내용은 해당 라이브러리의 문자 데이터입니다. CSS로 내용을 구문 분석하려면 추가 구문 분석기를 찾아야합니다. – Gordon

답변

2

:

// 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 : DOM 같은 라이브러리와 HTML 파일 밖으로 내용을 얻기

// 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. 
1

. 사례 1의 경우

+0

DOMElement는 ArrayAccess를 구현하거나 허용합니까? – Gordon

+0

이미 img 태그에 대한 해결책을 작성했습니다. 배경 CSS 이미지에 대한 나의 대답 만 제공합니다. – Yosef

관련 문제