repace을 이용한 정규식 모음
출처 : http://blog.naver.com/PostList.nhn?blogId=rzip84¤tPage=49
|@| 문자를 , 로 치환 = preg_replace("|\|@\||" , ', ' , ($kind));
PHP 정규표현식 예제
iframe 제거
$STRING=preg_replace("!<\/iframe>!is","",$STRING);
script 제거
$STRING=preg_replace("!<\/script>!is","",$STRING);
meta 제거
$STRING=preg_replace("!!is","",$STRING);
style 태그 제거
$STRING=preg_replace("!<\/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("/]*src=[\"']?([^>\"']+)[\"']?[^>]*>/i",$STRING,$RESULT);
preg_match_all("/]*src=[\"']?([^>\"']+)[\"']?[^>]*>/i",$STRING,$RESULT);
preg_match_all("/]*src=[\"']?([^>\"']+)[\"']?[^>]*>/i",$STRING,$RESULT);
호스트 추출
preg_match("/^(http:\/\/)?([^\/]+)/i","http://www.kaudo.com/index.html",$matches);
$host = $matches[2];
echo$matches[0]."
";
echo$matches[1]."
";
echo$matches[2]."
";
?>
$host = $matches[2];
echo$matches[0]."
";
echo$matches[1]."
";
echo$matches[2]."
";
?>
메뉴얼
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
출처 : http://blog.naver.com/PostList.nhn?blogId=rzip84¤tPage=49
'PHP' 카테고리의 다른 글
★ php ★ php에서 변수값을 함수이름으로 사용하여 함수 실행하는 방법 (0) | 2011.05.08 |
---|---|
UTF-8기준 , php에서 한글 자르는 방법 (0) | 2010.08.28 |
PHP 환경변수 정리 (0) | 2010.08.28 |
strtotime 함수를 이용한 시간 및 날짜 변경하는 방법 (0) | 2010.08.28 |
PHP 클래스 사용방법 정리 (1) | 2010.08.28 |