글 본문내용 중에 이미지 태그만 출력하고자 할때 사용합니다.


ex #1)

 

<?php
 /*
 # example )
 $str = "
&lt;img src='./images/logo.gif'&gt; logo:: &lt;img src='java
script:alert(document.cookie)'
&gt;";
 echo link_image_all($str);
 */
 
function img_ext()
 {
    return array(
        
'gif','jpe','jpg','jpeg','bmp','png','art','ani','bnr','cal',
       
'fax','hdp','mac','pbm','pcd','pct','pcx','pgm','png','ppm',
       
'psd','ras','tga','tif','tiff','wmf','cdr','cgm','cmk','cut',
       
'dcx','dib','drw','dxf','emf','eps','flc','fli','iff',
       
'lbm','wpg'
    
);
 }

 function 
link_image_all($str)
 {
    if(!empty(
$sttr))
    {
        return 
preg_replace("/&lt;.*?img.*?src=\s*?['\"]http:\/\/([0-9a-z-.\/~_]+\.(" implode("|"img_ext()) . "))['\"].*?&gt;/i""<img src=\"http://\\1\" />"$str);
    }
    return 
false;
 }
 ?>


ex #2)

 

<?php
 /*
 # example )
 $str = "<img src='./images/logo.gif'> logo:: <img src='javascript:
alert(document.cookie)'>";
 echo link_image($str);
 */
 
function img_ext()
 {
    return array( 
     
'gif','jpe','jpg','jpeg','bmp','png','art','ani','bnr','cal',
     
'fax','hdp','mac','pbm','pcd','pct','pcx','pgm','png','ppm',
     
'psd','ras','tga','tif','tiff','wmf','cdr','cgm','cmk','cut',
     
'dcx','dib','drw','dxf','emf','eps','flc','fli','iff','lbm',
     
'wpg'
    
);
 }

 function 
link_image($str)
 {
    if(!empty(
$sttr))
    {
        return 
preg_replace("/http:\/\/([0-9a-z-.\/~_]+\.(" implode("|"img_ext()) . "))/i""<img src=\"http://\\1\" />"$str);
    }
    return 
false;
 }
 ?>