php preg_replace 함수를 이용한 정규식 태그 필터링 예제
데모 - http://demo.widgets.co.kr/?c=64/81/82
매뉴얼 - http://kr2.php.net/manual/kr/function.preg-replace.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..." 따옴표 있을때
태그안의 width=, height= 속성 제거
$STRING=preg_replace("/ width=(\"|\')?\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("/^(http:\/\/)?([^\/]+)/i","http://www.naver.com/index.html",$matches);
$host = $matches[2];
echo$matches[0]."<br>";
echo$matches[1]."<br>";
echo$matches[2]."<br>";
출력 -
http://www.naver.com
http://
www.naver.com
데모 - http://demo.widgets.co.kr/?c=64/81/82
게시판 목록
개발자팁
질문은 QA에서 해주시기 바랍니다.
| 번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|---|
| 5006 | 기타 | 5년 전 | 1922 | ||
| 5005 | 기타 | 5년 전 | 2021 | ||
| 5004 | 기타 | 5년 전 | 2136 | ||
| 5003 | 기타 | 5년 전 | 2810 | ||
| 5002 | 기타 | 5년 전 | 2385 | ||
| 5001 | 기타 | 5년 전 | 2722 | ||
| 5000 | 기타 | 5년 전 | 4766 | ||
| 4999 | 기타 | 5년 전 | 2028 | ||
| 4998 | 기타 | 5년 전 | 2693 | ||
| 4997 | 기타 | 5년 전 | 2742 | ||
| 4996 | 기타 | 5년 전 | 2667 | ||
| 4995 | 기타 | 5년 전 | 2912 | ||
| 4994 | 기타 | 5년 전 | 2166 | ||
| 4993 | 기타 | 5년 전 | 2041 | ||
| 4992 | 기타 | 5년 전 | 1968 | ||
| 4991 | 기타 | 5년 전 | 1982 | ||
| 4990 | 기타 | 5년 전 | 2043 | ||
| 4989 | 기타 | 5년 전 | 2364 | ||
| 4988 | 기타 | 5년 전 | 2054 | ||
| 4987 | 기타 | 5년 전 | 2327 | ||
| 4986 | 기타 | 5년 전 | 2799 | ||
| 4985 | 웹서버 | 5년 전 | 4945 | ||
| 4984 | OS | 5년 전 | 3008 | ||
| 4983 | MySQL | 5년 전 | 2894 | ||
| 4982 | 기타 | 5년 전 | 2013 | ||
| 4981 | PHP | 5년 전 | 2655 | ||
| 4980 | 기타 | 5년 전 | 8974 | ||
| 4979 | 웹서버 | 5년 전 | 3074 | ||
| 4978 | 기타 | 5년 전 | 2051 | ||
| 4977 | PHP | 5년 전 | 3515 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기