2017-04-23 5 views
-5

스타일로 작성된 URL 인 경우 사이트의 사진에 대한 URL을 어떻게 복사합니까?PHP에서 웹 스크랩 사진

<div class="thumb"> 
       <a href="http://goruzont.blogspot.com/2017/04/blog-post_6440.html" style="background:url(https://1.bp.blogspot.com/-6vpIH5iqPYs/WPzlNdxsRpI/AAAAAAAAntU/d7U_Ch_6FiIPwosNL4tWwqBeXw8qwo2nACLcB/s1600/1424051.jpg) no-repeat center center;background-size:cover"> 

<span class="thumb-overlay"></span></a> 
</div> 
+0

모든 블록의 URL 사진을 복사하는 방법 " 무지"? –

답변

0

당신은 배경 이미지 또는 배경 추출하는 정규식을 사용할 수 있습니다 : URL 형식을

$str=' 
<div class="thumb"> 
       <a href="http://goruzont.blogspot.com/2017/04/blog-post_6440.html" style="background:url(https://1.bp.blogspot.com/-6vpIH5iqPYs/WPzlNdxsRpI/AAAAAAAAntU/d7U_Ch_6FiIPwosNL4tWwqBeXw8qwo2nACLcB/s1600/1424051.jpg) no-repeat center center;background-size:cover"> 

<span class="thumb-overlay"></span></a> 
</div>'; 

preg_match_all('~\bbackground(-image)?\s*:(.*?)\(\s*(\'|")?(?<image>.*?)\3?\s*\)~i',$str,$matches); 
$images = $matches['image']; 
foreach($images as $img){ 
    echo $img; 
} 
# https://1.bp.blogspot.com/-6vpIH5iqPYs/WPzlNdxsRpI/AAAAAAAAntU/d7U_Ch_6FiIPwosNL4tWwqBeXw8qwo2nACLcB/s1600/1424051.jpg 
+1

HTML 구문 분석에 정규식을 사용하지 마십시오. [토니 조랑말, 그는 온다.] (http://stackoverflow.com/a/1732454/1902010) – ceejayoz

+0

@ceejayoz 이유를 제공해 주셔서 감사합니다. 제가 확인하겠습니다. –

+0

도와 주셔서 감사합니다! 모든 블록 "엄지 손가락"에 URL을 복사하는 방법을 알려주시겠습니까? –

2

사용할 수 DOMDocumentDOMXPath

$html = <<< EOF 
<div class="thumb"> 
       <a href="http://goruzont.blogspot.com/2017/04/blog-post_6440.html" style="background:url(https://1.bp.blogspot.com/-6vpIH5iqPYs/WPzlNdxsRpI/AAAAAAAAntU/d7U_Ch_6FiIPwosNL4tWwqBeXw8qwo2nACLcB/s1600/1424051.jpg) no-repeat center center;background-size:cover"> 

<span class="thumb-overlay"></span></a> 
</div> 
EOF; 

libxml_use_internal_errors(true); 
$dom = new DOMDocument(); 
$dom->loadHTML($html); 
$xpath = new DOMXPath($dom); 

foreach ($xpath->query('//div[contains(@class,"thumb")]/a') as $item) { 

    $img_src = $item->getAttribute('style'); 
    $img_src = preg_replace('/.*?\((.*?)\).*?/m', '$1', $img_src); 
    print $img_src; 
} 

# https://1.bp.blogspot.com/-6vpIH5iqPYs/WPzlNdxsRpI/AAAAAAAAntU/d7U_Ch_6FiIPwosNL4tWwqBeXw8qwo2nACLcB/s1600/1424051.jpg 
+0

도와 주셔서 감사합니다! 모든 블록 "엄지 손가락"에 URL을 복사하는 방법을 알려주시겠습니까? –

+0

도움주세요, 질문은 동일합니다, 그냥 모든 블록 "엄지"의 URL 사진을 복사해야합니다. –