정규식을 이용한 이미지 링크 추출
페이지 정보
작성자 MintState 댓글 0건 조회 31,197회 작성일 08-11-10 13:28본문
정규식을 이용한 이미지 링크 추출
아래와 같은 본문이 있을시 여기서 이미지나 링크만 추출 하여 보자.
여러가지 방법이 있겠지만
1번째.
결과 값
2번째.
처음 나온는 이미지 추출(jpg,gif,png)
결과 값
http://xxxx.com/a.gif
3. 세번째.
특정 웹페이지를 읽어 그 페이지에 있는 이미지 추출
아래와 같은 본문이 있을시 여기서 이미지나 링크만 추출 하여 보자.
$str = "<a name='top'></a> <img src='http://xxxx.com/a.gif' border='0' alt=''> <img src='http://xxxx.com/b.png' border='0' alt=''> <a href='http://xxxx.com/gogo.html?no=2' target='_blank'><img src='http://xxxx.com/gif.php?no=5368753' border='0' alt=''></a>";
여러가지 방법이 있겠지만
1번째.
// a 링크만 추출하기
preg_match_all("|<a[^>]+>(.*)</a>|U",stripslashes($str),$out1, PREG_PATTERN_ORDER);
preg_match_all("|<a[^>]+>.*</a>|U",stripslashes($str),$out2, PREG_PATTERN_ORDER);
preg_match_all("^<a.*<\/a>^U", stripslashes($str), $out3);
// http 로 시작하는 것만추출
preg_match_all("((http)://[a-z0-9-]+.[][a-zA-Z0-9:@=_~%;?/.+-]+)",stripslashes($str),$out4, PREG_PATTERN_ORDER);
// 이미지만 추출
preg_match_all("/<img[^>]*src=[\"']?([^>\"']+)[\"']?[^>]*>/i", stripslashes($str), $out5);
print_r ($out1);
print_r ($out2);
print_r ($out3);
print_r ($out4);
print_r ($out5);
결과 값
Array
(
[0] => Array
(
[0] => <a name='top'></a>
[1] => <a href='http://xxxx.com/gogo.html?no=2' target='_blank'><img src='http://xxxx.com/gif.php?no=5368753' border='0' alt=''></a>
)
[1] => Array
(
[0] =>
[1] => <img src='http://xxxx.com/gif.php?no=5368753' border='0' alt=''>
)
)
Array
(
[0] => Array
(
[0] => <a name='top'></a>
[1] => <a href='http://xxxx.com/gogo.html?no=2' target='_blank'><img src='http://xxxx.com/gif.php?no=5368753' border='0' alt=''></a>
)
)
Array
(
[0] => Array
(
[0] => <a name='top'></a>
[1] => <a href='http://xxxx.com/gogo.html?no=2' target='_blank'><img src='http://xxxx.com/gif.php?no=5368753' border='0' alt=''></a>
)
)
Array
(
[0] => Array
(
[0] => http://xxxx.com/a.gif
[1] => http://xxxx.com/b.png
[2] => http://xxxx.com/gogo.html?no=2
[3] => http://xxxx.com/gif.php?no=5368753
)
[1] => Array
(
[0] => http
[1] => http
[2] => http
[3] => http
)
)
Array
(
[0] => Array
(
[0] => <img src='http://xxxx.com/a.gif' border='0' alt=''>
[1] => <img src='http://xxxx.com/b.png' border='0' alt=''>
[2] => <img src='http://xxxx.com/gif.php?no=5368753' border='0' alt=''>
)
[1] => Array
(
[0] => http://xxxx.com/a.gif
[1] => http://xxxx.com/b.png
[2] => http://xxxx.com/gif.php?no=5368753
)
)
2번째.
처음 나온는 이미지 추출(jpg,gif,png)
$photo = getImg($str);
print_r ($photo);
function getImg($content) {
$img = "";
preg_match("<img [^<>]*>", $content, $imgTag);
if($imgTag[0]){
if( stristr($imgTag[0], "http://") ) {
preg_match("/http:\/\/.*\.(jp[e]?g|gif|png)/Ui", $imgTag[0], $imgName);
$img = $imgName[0];
} else {
preg_match("/.*\.(jp[e]?g|gif|png)/Ui", $imgTag[0], $imgName);
$img = $imgName[0];
}
}
/*
if($imgTag) {
if( stristr($imgTag[2], "http://") ) {
preg_match("/http:\/\/.*\.(jp[e]?g|gif|png)/Ui", $imgTag[2], $imgName);
$img = $imgName[0];
} else {
preg_match("/.*\.(jp[e]?g|gif|png)/Ui", $imgTag[2], $imgName);
$img = $imgName[0];
}
}
*/
return $img;
}
결과 값
http://xxxx.com/a.gif
3. 세번째.
특정 웹페이지를 읽어 그 페이지에 있는 이미지 추출
<?php
$startPage = "1"; // 시작 페이지
$endPage = "2"; // 마지막 페이지
for($i=$startPage; $endPage+1 > $i;$i++)
{
$data = ""; // 초기화
$datafile = "http://xxxx.com/photo.html?page=$i"; // 리스트 페이지
$fp = @fopen($datafile, "r");
while (!feof ($fp))
{
$data .= fgets($fp);
}
fclose($fp);
preg_match_all("/<img[^>]*src=[\"']?([^>\"']+)[\"']?[^>]*>/i",$data, $matches);
foreach($matches as $key => $value)
{
foreach($value as $key_2 => $value_2)
{
//$value_2 = ereg_replace(".thumb","",$value_2);
//$value_2 = ereg_replace("img src=","",$value_2);
echo $value_2."<br />";
}
break;
}
}
?> |
|
댓글목록
등록된 댓글이 없습니다.





정규식을 이용한 이미지 링크 추출