코딩공주
php코드에서 혹시 <br>과 같은 html무시 / CRLF(개행문자) 및 특정 태그 제거 본문
1. 정규식을 통한 제거
1 | $text = preg_replace( '/\r\n|\r|\n/' , '' , $text ); |
2. 문자열 함수사용으로 제거
1 2 3 | $text = str_replace ( array ( "\r\n" , "\r" , "\n" ), '' , $text ); 또는 $text = strtr ( $text , array ( "\r\n" => '' , "\r" => '' , "\n" => '' )); |
html 태그 제거
1 | $content = preg_replace( "(\<(/?[^\>]+)\>)" , "" , $content ); |
textarea 제거
1 2 | $content = preg_replace( "!<textarea(.*?)>!is" , "[textarea]" , $content ); $content = preg_replace( "!</textarea(.*?)>!is" , "[/textarea]" , $content ); |
script 제거
1 | $str =preg_replace( "!<script(.*?)<\/script>!is" , "" , $str ); |
iframe 제거
1 | $str =preg_replace( "!<iframe(.*?)<\/iframe>!is" , "" , $str ); |
meta 제거
1 | $str =preg_replace( "!<meta(.*?)>!is" , "" , $str ); |
style 태그 제거
1 | $str =preg_replace( "!<style(.*?)<\/style>!is" , "" , $str ); |
를 공백으로 변환
1 | $str = str_replace ( " " , " " , $str ); |
연속된 공백 1개로
1 | $str =preg_replace( "/\s{2,}/" , " " , $str ); |
태그안에 style= 속성 제거
1 2 | $str =preg_replace( "/ zzstyle=([^\"\']+) /" , " " , $str ); // style=border:0 따옴표가 없을때 $str =preg_replace( "/ style=(\"|\')?([^\"\']+)(\"|\')?/" , "" , $str ); // style="border:0" 따옴표 있을때 |
태그안의 width=, height= 속성 제거
1 2 | $str =preg_replace( "/ width=(\"|\')?\d+(\"|\')?/" , "" , $str ); $str =preg_replace( "/ height=(\"|\')?\d+(\"|\')?/" , "" , $str ); |
img 태그 추출 src 추출
1 2 | preg_match( "/<img[^>]*src=[\"']?([^>\"']+)[\"']?[^>]*>/i" , $str , $result ); preg_match_all( "/<img[^>]*src=[\"']?([^>\"']+)[\"']?[^>]*>/i" , $str , $result ); |
특수문자 제거
1 | $string = preg_replace( "/[ #\&\+\-%@=\/\\\:;,\.'\"\^`~\_|\!\?\*$#<>()\[\]\{\}]/i" , "" , $string ); |
공백제거
1 2 | $string = preg_replace( '/ /' , '' , $string ); $string = preg_replace( "/\s+/" , "" , $string ); |
반복 입력된 단어 제거
1 | $string = preg_replace( "/s(w+s)1/i" , "$1" , $string ); |
반복 입력된 부호 제거
1 | $string = preg_replace( "/.+/i" , "." , $string ); |
영문자를 제외한 모든 문자 제거
1 | $string = preg_replace( "/[^A-Za-z]/" , "" , $string ); |
영문자와 공백문자(Space)를 제외한 모든 문자를 제거
1 | $string = preg_replace( "/[^A-Za-z|\x20]/" , "" , $string ); |
ASCII 범주 코드 영문+특수문자를 제외한 모든 문자를 제거
1 | $string = preg_replace( "/[^\x20-\x7e]/" , "" , $string ); |
img 태그 추출
1 2 | preg_match_all( "/<img[^>]*src=[\'\"]?([^>\'\"]+)[\'\"]?[^>]*>/" , $img , $matchs ); print_r( $matchs ); |
출처: http://chongmoa.com/php/97908
'코딩정보' 카테고리의 다른 글
반응형 크기값 단위 / Viewport 단위 :vh, vw, vmin, vmax (0) | 2019.02.13 |
---|---|
transform 가운데 배치 (0) | 2019.02.13 |
javaScript - <a/> 태그 onClick 이벤트 주기 bind() 그리고 e.preventDefault() (0) | 2019.01.22 |
javascript로 클래스 제어하기(classList) .classList.toggle와.toggleClass차이 (0) | 2019.01.17 |
NaN(x)과 자바스크립트로 숫자형과 그외 자료형 체크하기 (0) | 2019.01.15 |
Comments