프로그래밍/php

정규식 예제

verdana 2018. 2. 27. 11:57
// 7,8자리 숫자 추출 전부
preg_match_all("/[0-9]{7,8}/", $body_data_st, $matches);
print_r($matches);


// 특정문자열부터 시작해서 특정문자열까지 추출
preg_match("/추출시작문자열 :([^추출종료문자열]*)/is", $str, $matches);


// 괄호() 포함 삭제
$str = preg_replace("/\(([^\(\)]+)\)/", "", $str);


// 예: 블라블라 1회 또는 블라블라 1화 삭제
$str = preg_replace("/([0-9]{1,}(^[0-9])*회)|([0-9]{1,}(^[0-9])*화)|([0-9]{1,}(^[0-9])*편)|([0-9]{1,}(^[0-9])*부)/i", "", $str);


// 예 블라블라 제1회 삭제
$str = preg_replace("/_제[0-9]([^회]*회)/i", "", $str);


// 맨뒤에 숫자 삭제
$str = preg_replace("/[0-9]{1,}$/i", "", $str);


// 한글 포함여부
if ( preg_match("/[\xA1-\xFE][\xA1-\xFE]/", $str) ) {
//포함
}