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
게시글 목록
| 번호 | 제목 |
|---|---|
| 17043 | |
| 17042 |
JavaScript
for 문으로 유사패턴 태그 만들기
4
|
| 17025 |
JavaScript
with 블럭 사용하기
17
|
| 17015 | |
| 17001 |
JavaScript
event listeners 찾기
3
|
| 16998 | |
| 16994 | |
| 16988 | |
| 16985 |
JavaScript
자바스크립트 for length / for in / for of
6
|
| 16983 | |
| 16981 | |
| 16979 | |
| 16978 | |
| 16976 | |
| 16968 | |
| 16967 |
JavaScript
상위요소 찾기 노드
3
|
| 16957 | |
| 16956 | |
| 16955 | |
| 16951 | |
| 16950 | |
| 16949 | |
| 16932 |
기타
postman
4
|
| 16930 | |
| 16913 | |
| 16909 | |
| 16906 | |
| 16905 | |
| 16904 | |
| 16894 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기