2011-12-14 3 views
1
$str = "html code here and img tag"; 
preg_match_all('~<img ([^>]+)>~i', $str, $matches); 
$images = array(); 
foreach ($matches[1] as $str) { 
    preg_match_all('~([a-z]([a-z0-9]*)?)=("|\')(.*?)("|\')~is', $str, $pairs); 
} 

IMG에서 SRC를 가져올 수 없습니까?HTML 이미지 태그에서 src = ""를 가져 오는 방법

+1

참조 : http://stackoverflow.com/questions/1058852/regex-to-get-src-value-from-an-img-tag – Andy

답변

3

정규 표현식으로 img src 태그를 구문 분석하는 대신 simplehtmldom을 사용하는 것이 좋습니다.

$html = file_get_html('...html...'); 

// Find all images & echo src 
foreach($html->find('img') as $element) 
     echo $element->src . '<br>'; 
2

PHP에서 Simple HTML Dom과 같은 XML 구문 분석기를 사용할 수도 있고, 이와 같은 방식의 정규식을 사용할 수도 있습니다.

/src="([^"]*)"/i 
관련 문제