PHP 정규표현식 예제
iframe 제거
$STRING=preg_replace("!<iframe(.*?)<\/iframe>!is","",$STRING);
script 제거
$STRING=preg_replace("!<script(.*?)<\/script>!is","",$STRING);
meta 제거
$STRING=preg_replace("!<meta(.*?)>!is","",$STRING);
style 태그 제거
$STRING=preg_replace("!<style(.*?)<\/style>!is","",$STRING);
를 공백으로 변환
$STRING=str_replace(" "," ",$STRING);
연속된 공백 1개로
$STRING=preg_replace("/\s{2,}/"," ",$STRING);
태그안에 style= 속성 제거
$STRING=preg_replace("/ style=([^\"\']+) /"," ",$STRING); // style=border:0... 따옴표가 없을때
$STRING=preg_replace("/ style=(\"|\')?([^\"\']+)(\"|\')?/","",$STRING); // style="border:0..." 따옴표 있을때
$STRING=preg_replace("/ style=(\"|\')?([^\"\']+)(\"|\')?/","",$STRING); // style="border:0..." 따옴표 있을때
태그안의 width=, height= 속성 제거
$STRING=preg_replace("/ width=(\"|\')?\d+(\"|\')?/","",$STRING);
$STRING=preg_replace("/ height=(\"|\')?\d+(\"|\')?/","",$STRING);
$STRING=preg_replace("/ height=(\"|\')?\d+(\"|\')?/","",$STRING);
img 태그 추출 src 추출
preg_match("/<img[^>]*src=[\"']?([^>\"']+)[\"']?[^>]*>/i",$STRING,$RESULT);
preg_match_all("/<img[^>]*src=[\"']?([^>\"']+)[\"']?[^>]*>/i",$STRING,$RESULT);
preg_match_all("/<img[^>]*src=[\"']?([^>\"']+)[\"']?[^>]*>/i",$STRING,$RESULT);
호스트 추출
<?
preg_match("/^(http:\/\/)?([^\/]+)/i","http://www.kaudo.com/index.html",$matches);
$host = $matches[2];
echo$matches[0]."<br>";
echo$matches[1]."<br>";
echo$matches[2]."<br>";
?>
preg_match("/^(http:\/\/)?([^\/]+)/i","http://www.kaudo.com/index.html",$matches);
$host = $matches[2];
echo$matches[0]."<br>";
echo$matches[1]."<br>";
echo$matches[2]."<br>";
?>
http://www.kaudo.com
http://
www.kaudo.com
http://
www.kaudo.com
매뉴얼:
http://kr2.php.net/manual/kr/function.preg-replace.php
http://kr2.php.net/manual/kr/function.preg-match.php
http://kr2.php.net/manual/kr/function.str-replace.php
'SCRIPTING' 카테고리의 다른 글
| php urlencode, urldecode, mb_convert_encoding utf8 쿼리스트링 (0) | 2009/06/26 |
|---|---|
| php ffmpeg command example on windows (0) | 2009/06/25 |
| [PHP] REGULAR EXPRESSION EXAMPLE preg_replace, preg_match, str_replace (1) | 2009/06/25 |
| asp binary 바이너리 파일 입출력 함수 (0) | 2009/06/23 |
| c++ win32 api char, string 키코드 알아내기 (0) | 2009/06/21 |
| mssql to mysql process log (0) | 2009/06/17 |


댓글을 달아 주세요
님은 좀 짱인듯o_O)b
2009/08/26 17:51 [ ADDR : EDIT/ DEL : REPLY ]