2012-07-12 2 views
0

게시물의 모든 이미지를 얻을으로, preg_match_all를 사용하여이 같은 비트 보이는 방법 :내가 그 안에 여러 개의 이미지 (때로는 때로는 때로는 하나, 둘, 셋)이있는 블로그 항목이

<a href="http://xxx/" rel="attachment w133> 
    <img class="yyy" title="title1" src="http://xxx/title1.jpg" alt="" width="650" height="487" /> 
</a> 
<a href="http://xxx/" rel="attachment w134"> 
    <img class="yyy" title="title2" src="http://xxx/title2.jpg" alt="" width="650" height="487" /> 
</a> 
<a href="http://xxx/" rel="attachment w135"> 
    <img class="yyy" title="title3" src="http://xxx/title3.jpg" alt="" width="650" height="487" /> 
</a> 

과를 이것에 뒤 따르는 일부 텍스트.

이제 어떻게 preg_match_all을 사용하여 첫 번째 부분을 추출 할 수 있는지 궁금합니다. 이제는 PHP 프로그래밍에 대해서는 다소 어리지만, preg_match_all은 사용하지 않았습니다. 여기

이 코드는 충분하지 않은, 마지막 이미지를 추출하지 : 사람이 전혀 가능하다면, 그것을 달성하는 방법의 힌트 나에게 줄 수 있다면

$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post_content, $matches); 

그것은 좋은 것입니다. 정말 고마워!

+0

. 어떤 문자와도 일치하므로 대신 [^ <>]을 사용해 이미지 태그 – Waygood

+0

에 머물러있게하십시오. [HTML 파서] (http://us2.php.net/manual/en/class. domdocument.php)를 사용합니다. 그런 다음 XPath 쿼리로 이미지를 가져올 수 있습니다. – Wiseguy

+0

@Waygood. 고마워. 아마도 내가 다음과 같은 것을 사용한다면 : ('/ ') 물건을 추출하겠습니까? – luftikus143

답변

3
$post_content='<a href="http://xxx/" rel="attachment w133> 
    <img class="yyy" title="title1" src="http://xxx/title1.jpg" alt="" width="650" height="487" /> 
</a> 
<a href="http://xxx/" rel="attachment w134"> 
    <img class="yyy" title="title2" src="http://xxx/title2.jpg" alt="" width="650" height="487" /> 
</a> 
<a href="http://xxx/" rel="attachment w135"> 
    <img class="yyy" title="title3" src="http://xxx/title3.jpg" alt="" width="650" height="487" /> 
</a> 
'; 

preg_match_all('/<a\s[^>]*href=([\"\']??)([^\" >]*?)\\1[^>]*>(.*)<\/a>/siU', $post_content, $matches); 
//print_r ($matches);//$matches - array which contains all your images 
print $matches[0][0]; //first link with image 
print $matches[0][1]; //second link with image 
print $matches[0][2]; //third link with image 

출력 :

<a href="http://xxx/" rel="attachment w133&gt; 
    &lt;img class=" yyy"="" title="title1" src="http://xxx/title1.jpg" alt="" width="650" height="487"> 
</a> 
<a href="http://xxx/" rel="attachment w134"> 
    <img class="yyy" title="title2" src="http://xxx/title2.jpg" alt="" width="650" height="487"> 
</a> 
<a href="http://xxx/" rel="attachment w135"> 
    <img class="yyy" title="title3" src="http://xxx/title3.jpg" alt="" width="650" height="487"> 
</a> 
+0

그건 내가 말한 것과 똑같은거야, 안 그래? 그것은이 결과 : 배열 ( [0] => 배열 ( [0] => ) [1] => 배열 ( [0] => HTTP : // xxx/title2.jpg ) – luftikus143

+0

정확히 원하는 것만 둡니다. 마지막 이미지 만 가져옵니다. 그런 다음 도 얻지 못합니다. 그러나 내가 필요하면 확실하지 않습니다. – luftikus143

+0

내 대답을 편집했습니다. 지금 그것은 당신이 원하는 것을 수행합니다 – Alex

0

시도 :

preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"][^\/>]*>/Ui', $post_content, $matches); 
print_r($matches); 
+1

이 결과는 "Array ([0] => Array() [1] => Array())" – luftikus143

관련 문제