2015-01-27 3 views
-1

나는 웹 사이트를 구문 분석하고 이미지의 이름이나 URL을 잡으려고합니다.PHP Preg_match 이미지 URL에서

예 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 
) 

출력은 다음과 같습니다

$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] : Tag nav가 Entity에서 유효하지 않습니다.

+0

당신은 당신이 DOM 파서 경험이없는 말, 그러나 이것은 잘 사용하는 방법을 배울 수있는 완벽한 시간이 될 수 있습니다. 미래에 HTML을 다시 구문 분석하기 위해 정규 표현식을 사용하는 단점을 보게 될 가능성이 있습니다. 아마도이 프로젝트에서도 마찬가지 일 것입니다. Regexes는 임의의 HTML을 파싱하는 작업까지는하지 않습니다. –

답변

0

이 정규식을 사용해 볼 수 있습니다.

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

는 출력 : DOM 아무것도에서

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 
) 
+0

감사합니다.그게 효과가있는 것 같습니다. – user1443402

+0

당신은 환영합니다 :) – MH2K9

+0

사실 내가 필요로하는 이미지를 파싱합니다 : '/images/products/display/GR14_EXTRALARGE.jpg' 작은 따옴표를 사용하고 싶지 않아요 – user1443402

4

도, 노드의 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가 종료 허용 복잡한 조건. 다음 예제에서는 imgsrc 특성을 td 내부에 출력합니다.

$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을 포함하고, 더 중요한 imgid 속성과 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 
+0

시도한이 오류가있어 : DOMDocument :: loadHTML() [domdocument.loadhtml] : 태그 이동이 Entity에서 유효하지 않습니다. 코드의 첫 번째 줄 앞에 $ html = file_get_contents ($ url)를 사용하여 구문 분석 할 HTML을 지정하십시오. – user1443402

+0

HTML이 완전히 유효하지 않기 때문에 경고입니다 4 ('nav'는 HTML5 태그입니다), 'libxml_use_internal_errors (참)'. – ThW

+0

초기 질문과 두 가지 제안 사항이 모두 업데이트되었습니다. – user1443402