본문 바로가기

Programming/PHP&HTML

정규식으로 이미지 파일만 출력하기


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

아래와 같은 본문이 있을시 여기서 이미지나 링크만 추출 하여 보자.
1 $str = "<a name='top'></a>
2 <img src='http://xxxx.com/a.gif' border='0' alt=''>
3 <img src='http://xxxx.com/b.png' border='0' alt=''>
4 <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번째.
01 // a 링크만 추출하기
02 preg_match_all("|<a[^>]+>(.*)</a>|U",$str,$out1, PREG_PATTERN_ORDER); 
03 preg_match_all("|<a[^>]+>.*</a>|U",$str,$out2, PREG_PATTERN_ORDER); 
04 preg_match_all("^<a.*<\/a>^U", $str, $out3);
05   
06 // http 로 시작하는 것만추출
07 preg_match_all("((http)://[a-z0-9-]+.[][a-zA-Z0-9:&#@=_~%;?/.+-]+)",$str,$out4, PREG_PATTERN_ORDER); 
08   
09 // 이미지만 추출
10 preg_match_all("/<img[^>]*src=[\"']?([^>\"']+)[\"']?[^>]*>/i", $str, $out5);
11   
12 print_r ($out1);
13 print_r ($out2);
14 print_r ($out3);
15 print_r ($out4);
16 print_r ($out5);


결과 값
01 Array
02 (
03     [0] => Array
04         (
05             [0] => <a name='top'></a>
06             [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>
07         )
08   
09     [1] => Array
10         (
11             [0] => 
12             [1] => <img src='http://xxxx.com/gif.php?no=5368753' border='0' alt=''>
13         )
14   
15 )
16 Array
17 (
18     [0] => Array
19         (
20             [0] => <a name='top'></a>
21             [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>
22         )
23   
24 )
25 Array
26 (
27     [0] => Array
28         (
29             [0] => <a name='top'></a>
30             [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>
31         )
32   
33 )
34 Array
35 (
36     [0] => Array
37         (
38             [0] => http://xxxx.com/a.gif
39             [1] => http://xxxx.com/b.png
40             [2] => http://xxxx.com/gogo.html?no=2
41             [3] => http://xxxx.com/gif.php?no=5368753
42         )
43   
44     [1] => Array
45         (
46             [0] => http
47             [1] => http
48             [2] => http
49             [3] => http
50         )
51   
52 )
53 Array
54 (
55     [0] => Array
56         (
57             [0] => <img src='http://xxxx.com/a.gif' border='0' alt=''>
58             [1] => <img src='http://xxxx.com/b.png' border='0' alt=''>
59             [2] => <img src='http://xxxx.com/gif.php?no=5368753' border='0' alt=''>
60         )
61   
62     [1] => Array
63         (
64             [0] => http://xxxx.com/a.gif
65             [1] => http://xxxx.com/b.png
66             [2] => http://xxxx.com/gif.php?no=5368753
67         )
68   
69 )


2번째.
처음 나온는 이미지 추출(jpg,gif,png)
01 $photo = getImg($str);
02 print_r ($photo);
03 function getImg($content) {
04     $img = "";
05     preg_match("<img [^<>]*>", $content, $imgTag);
06       
07     if($imgTag[0]){ 
08         if( stristr($imgTag[0], "http://") ) {
09             preg_match("/http:\/\/.*\.(jp[e]?g|gif|png)/Ui", $imgTag[0], $imgName);
10             $img = $imgName[0];
11         } else {
12             preg_match("/.*\.(jp[e]?g|gif|png)/Ui", $imgTag[0], $imgName);
13             $img = $imgName[0];
14         }
15     }
16     /*
17     if($imgTag) {
18         if( stristr($imgTag[2], "http://") ) {
19             preg_match("/http:\/\/.*\.(jp[e]?g|gif|png)/Ui", $imgTag[2], $imgName);
20             $img = $imgName[0];
21         } else {
22             preg_match("/.*\.(jp[e]?g|gif|png)/Ui", $imgTag[2], $imgName);
23             $img = $imgName[0];
24         }
25     }
26     */
27     return $img;
28 }


결과 값
http://xxxx.com/a.gif

3. 세번째.
특정 웹페이지를 읽어 그 페이지에 있는 이미지 추출
01 <?php
02 $startPage  = "1";      // 시작 페이지
03 $endPage    = "2";     // 마지막 페이지
04   
05 for($i=$startPage; $endPage+1 > $i;$i++)
06 {
07     $data           = "";   // 초기화
08     $datafile       = "http://xxxx.com/photo.html?page=$i"; // 리스트 페이지
09     $fp             = @fopen($datafile, "r");
10     while (!feof ($fp))
11     {
12         $data .= fgets($fp);
13     }
14     fclose($fp);
15   
16     preg_match_all("/<img[^>]*src=[\"']?([^>\"']+)[\"']?[^>]*>/i",$data, $matches);
17   
18     foreach($matches as $key => $value)
19     {
20         foreach($value as $key_2 => $value_2)
21         {
22             //$value_2 =  ereg_replace(".thumb","",$value_2);
23             //$value_2 =  ereg_replace("img src=","",$value_2);
24             echo $value_2."<br />";
25         }
26         break;
27     }
28 }
29 ?>