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에서 해주시기 바랍니다.
| 번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|---|
| 26 | MySQL |
아우겐나이스
|
21년 전 | 5829 | |
| 25 | JavaScript |
아우겐나이스
|
21년 전 | 5210 | |
| 24 | MySQL | 21년 전 | 7769 | ||
| 23 | MySQL | 21년 전 | 5065 | ||
| 22 | 기타 | 21년 전 | 8062 | ||
| 21 | 기타 | 21년 전 | 5968 | ||
| 20 | MySQL | 21년 전 | 6606 | ||
| 19 | MySQL | 21년 전 | 9361 | ||
| 18 | 기타 | 21년 전 | 6948 | ||
| 17 | MySQL | 21년 전 | 9483 | ||
| 16 | JavaScript | 21년 전 | 6728 | ||
| 15 | 기타 | 21년 전 | 4755 | ||
| 14 | 기타 | 21년 전 | 7664 | ||
| 13 | 기타 | 21년 전 | 5466 | ||
| 12 | JavaScript |
Recluse
|
21년 전 | 6486 | |
| 11 | 기타 |
아우겐나이스
|
21년 전 | 5419 | |
| 10 | 기타 |
Recluse
|
21년 전 | 5062 | |
| 9 | 기타 | 21년 전 | 5932 | ||
| 8 | MySQL | 21년 전 | 7458 | ||
| 7 | PHP | 21년 전 | 6935 | ||
| 6 | JavaScript | 21년 전 | 8166 | ||
| 5 | PHP | 21년 전 | 6189 | ||
| 4 | Linux | 21년 전 | 7063 | ||
| 3 | PHP | 21년 전 | 7306 | ||
| 2 | 기타 | 21년 전 | 5656 | ||
| 1 | JavaScript | 22년 전 | 7667 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기