본문으로 바로가기

★ php 정규식 ★

category [ Web 관련 ]/php 2021. 7. 28. 17:42

TEXTAREA 제거
$content = preg_replace("!<TEXTAREA(.*?)>!is","[TEXTAREA]",$content);
$content = preg_replace("!</TEXTAREA(.*?)>!is","[/TEXTAREA]",$content);

 


script 제거
$str=preg_replace("!<script(.*?)<\/script>!is","",$str);

 

 

iframe 제거
$str=preg_replace("!<iframe(.*?)<\/iframe>!is","",$str);

 

meta 제거
$str=preg_replace("!<meta(.*?)>!is","",$str);

 

style 태그 제거
$str=preg_replace("!<style(.*?)<\/style>!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("/<img[^>]*src=[\"']?([^>\"']+)[\"']?[^>]*>/i",$str,$RESULT);
preg_match_all("/<img[^>]*src=[\"']?([^>\"']+)[\"']?[^>]*>/i",$str,$RESULT);

 

a 태그에서 title 추출

$pattern ="/<a[^>]*title=[\"']?([^>\"']+)[\"']?[^>]*>/i";

 

a 태그에서 href 추출하는데 특정 순서만

$pattern ='/(href=)(\'|\")?([^<>\s\'\"]*)(\'|\"|\s|)/i';
preg_match_all($pattern, $linkArea[1][$z], $tmpLink);
print_r('link:'.$tmpLink[3][5].'<br>');

 

a 태그에서 내용 텍스트 추출

$pattern = '/<a(.*?)>(.*?)<\/a>/i';
preg_match($pattern, $value, $tmpTitle);

 

호스트 추출
<?
preg_match("/^(http:\/\/)?([^\/]+)/i","http://www.naver.com/index.php",$matches);
$host = $matches[2];
echo$matches[0]."<br>";
echo$matches[1]."<br>";
echo$matches[2]."<br>";
?>

 

[php] html tag 제거 정규식

$content = preg_replace("(\<(/?[^\>]+)\>)", "", $content);

 

이미지만 추출하는 정규식

preg_match_all("/<img[^>]*src=[\"']?([^>\"']+)[\"']?[^>]*>/i", $contents, $matches);

 

 

특정 엘리멘트 밑에 추출 정규식

preg_match('/<div id="my2">(.*?)<\/div>/is', $contents, $html);

preg_match("/\<span id=\"moonseller\"\>[^>]\<\/span\>/", $contents, $html);

 

 

style 태그 제거 정규식

$str=preg_replace("!<style(.*?)<\/style>!is","",$str);

 

 

특정 태그 제거 정규식

$contents = preg_replace("/<a[^>]*>/i", '', $contents);

$contents = preg_replace("/<\/a>/i", '', $contents);

 

 

 

 

 

 

'[ Web 관련 ] > php' 카테고리의 다른 글

php 요일 구하기, 현재 날짜 시간  (0) 2021.08.06
php JSON 한글 인코딩  (0) 2021.08.04
php 배열의 정렬 - 1차원 배열, 다차원 배열  (0) 2021.07.26
php 크롤링 (simple_html_dom, Snoopy)  (0) 2021.07.22
php ajax 샘플  (0) 2021.05.28