본문 바로가기

Developement/Web Programming

PHP 정규식

// textarea 제거
$str = preg_replace("!!is", "[textarea]", $str);
$str = preg_replace("!
!is", "[/textarea]", $str);


// script 제거​
$str = preg_replace("!!is", "", $str);


// iframe 제거
$str = preg_replace("!!is", "", $str);


// meta 제거
$str = preg_replace("!!is", "", $str);


// style 제거
$str = preg_replace("!!is", "", $str);


//  를 공백으로 변환
$str = str_replace(" ", " ", $str);


// 연속된 공백을 1개로 변환
$str = preg_replace("/\s{2,}/", " ", $str);


// 태그안에 style= 속성 제거
$str = preg_replace("/ zzstyle=([^\"\']+) /", " ", $str); // style=border:0... 따옴표가 없을때
$str = preg_replace("/ style=(\"|\')?([^\"\']+)(\"|\')?/", "", $str); // style="border:0..." 따옴표 있을때


// 태그안의 width=, height= 속성 제거
$str = preg_replace("/ width=(\"|\')?\d+(\"|\')?/", "", $str);
$str = preg_replace("/ height=(\"|\')?\d+(\"|\')?/", "", $str);


// img 태그 추출 src 추출
preg_match("/]*src=[\"']?([^>\"']+)[\"']?[^>]*>/i", $str, $RESULT);
preg_match_all("/]*src=[\"']?([^>\"']+)[\"']?[^>]*>/i", $str, $RESULT);


// 호스트 추출
preg_match("/^(http:\/\/)?([^\/]+)/i", "http://www.naver.com", $matches);