Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

코딩공주

php코드에서 혹시 <br>과 같은 html무시 / CRLF(개행문자) 및 특정 태그 제거 본문

코딩정보

php코드에서 혹시 <br>과 같은 html무시 / CRLF(개행문자) 및 특정 태그 제거

소수소수 2019. 2. 13. 13:16

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);


&nbsp;를 공백으로 변환

1
$str=str_replace("&nbsp;"," ",$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

Comments