2023. 3. 10. 11:08

php 문자열 제거 각종 정규식

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

 

C.m.A API 이야기(Mokulsha!)

1. 정규식을 통한 제거 $text = preg_replace('/||/','',$text); 2. 문자열 함수사용으로 제거 $text = str_replace(array("","",&quo..

chongmoa.com

 

2022. 7. 21. 16:17

C# 특정문자열로 둘러쌓인 문자열 추출

만일 문자열 중 %%...%%로 되어 있는 부분의 내용만 따로 꺼내고 싶으면 아래와 같이 하면 됨

적용 정규식 : %%([^%%]+)%%

이렇게 하고 c#의 Match.groups[1].value로 가져옴

string strFileSrc = "이것은 %%시험_111%% 문자열입니다";
MatchCollection mc = Regex.Matches(strFileSrc, "%%([^%%]+)%%");
List<string> lstrKeywordList = new List<string>();

foreach (Match match in mc)
{
	lstrKeywordList.Add(match.Groups[1].Value);
    //Console.WriteLine("단어 : {0}", match.Groups[1]);
}

 

 

 

2022. 6. 7. 16:30

소스 중간에 /* */ 주석 찾는 정규식

아래처럼 검색하면 대강 찾아짐...;;

\/\*[\x00-\xFFㄱ-ㅎ가-힣\s]*?\*\/

 

2015. 2. 9. 21:55

editplus text감싸기(따옴표로 감싸기 등)

EditPlus에서 문자열을 대상으로 앞뒤에 특정 문자로 감싸야 하는 경우가 있음. 

예를 들면 문자들 앞뒤로 ' 라던가 " 로 감싸는 경우


12345

54321

13579


이렇게 되어 있을때, 찾기에서 정규식쪽에 체크하고

찾을말 : ^(.+)

바꿀말 : "\1"


이렇게 하고 바꾸기 하면 


"12345"

"54321"

"13579"


이렇게 변경이 됨


바꿀말의 \1 은 찾을말에서 찾은 항목이며, 그 앞뒤로 내용을 넣으면 내용을 넣은 것으로 변경됨



2010. 3. 10. 17:45

자바 정규식을 이용해 문자열 변경

자바의 replaceAll 함수를 이용해서 문자열의 뒤의 4글자를 *로 만드는 정규식

"aaaaaaaa".replaceAll("[\\w]{4}$","****")

숫자를 바꾸거나 * 글자를 바꾸면 다른 일도 가능. 저거 보다 짧으면 살짝 오류가 생기는거 같은 느낌이 들지만, 뭐 4글자보다 긴걸 쓰던가;