홍길1234동abc입!!_#니다
라는 문구가 있다고 했을때 해당 문구에서
홍길동입니다
1234
abc
!!_#
를 각각 추출해내는 함수이다.
euckr을 기준으로 작성된 함수이며 euckr에서는 한글 패턴의 추출이 어려운 관계로
UTF-8로 전환하여 변환하는 형태이다.
핵심은 1 한글,2 영문 ,4 숫자 ,8 특수기호로 명시하고 처리하는 식이다.
========= 내용 ==============
$msg = "홍길1234동abc입!!_#니다";
function getMsgArr($msg) {
$convMsg = mb_convert_encoding($msg, "UTF-8", "EUC-KR");
$resultArr = array();
// 1: 한글
$pattern = '/[\x{1100}-\x{11FF}\x{3130}-\x{318F}\x{AC00}-\x{D7AF}]+/u';
preg_match_all($pattern,$convMsg,$match);
$resultArr[1] = mb_convert_encoding(implode('',$match[0]),"EUC-KR", "UTF-8");
// 2: 영문
$pattern = '/[a-zA-Z]/';
preg_match_all($pattern,$convMsg,$match);
$resultArr[2] = mb_convert_encoding(implode('',$match[0]),"EUC-KR", "UTF-8");
// 4: 숫자
$pattern = '/[0-9]/';
preg_match_all($pattern,$convMsg,$match);
$resultArr[4] = mb_convert_encoding(implode('',$match[0]),"EUC-KR", "UTF-8");
// 8: 특수기호
$pattern = '/[^\x{1100}-\x{11FF}\x{3130}-\x{318F}\x{AC00}-\x{D7AF}0-9a-zA-Z]+/u';
preg_match_all($pattern,$convMsg,$match);
$resultArr[8] = mb_convert_encoding(implode('',$match[0]),"EUC-KR", "UTF-8");
// 3: 한글 + 영문
$pattern = '/[\x{1100}-\x{11FF}\x{3130}-\x{318F}\x{AC00}-\x{D7AF}a-zA-Z]+/u';
preg_match_all($pattern,$convMsg,$match);
$resultArr[3] = mb_convert_encoding(implode('',$match[0]),"EUC-KR", "UTF-8");
// 5: 한글 + 숫자
$pattern = '/[\x{1100}-\x{11FF}\x{3130}-\x{318F}\x{AC00}-\x{D7AF}0-9]+/u';
preg_match_all($pattern,$convMsg,$match);
$resultArr[5] = mb_convert_encoding(implode('',$match[0]),"EUC-KR", "UTF-8");
// 9: 한글 + 특수기호
$pattern = '/[^0-9a-zA-Z]/';
preg_match_all($pattern,$convMsg,$match);
$resultArr[9] = mb_convert_encoding(implode('',$match[0]),"EUC-KR", "UTF-8");
// 6: 영문 + 숫자
$pattern = '/[0-9a-zA-Z]/';
preg_match_all($pattern,$convMsg,$match);
$resultArr[6] = mb_convert_encoding(implode('',$match[0]),"EUC-KR", "UTF-8");
// 10: 영문 + 특수기호
$pattern = '/[^\x{1100}-\x{11FF}\x{3130}-\x{318F}\x{AC00}-\x{D7AF}0-9]+/u';
preg_match_all($pattern,$convMsg,$match);
$resultArr[10] = mb_convert_encoding(implode('',$match[0]),"EUC-KR", "UTF-8");
// 12: 숫자 + 특수기호
$pattern = '/[^\x{1100}-\x{11FF}\x{3130}-\x{318F}\x{AC00}-\x{D7AF}a-zA-Z]+/u';
preg_match_all($pattern,$convMsg,$match);
$resultArr[12] = mb_convert_encoding(implode('',$match[0]),"EUC-KR", "UTF-8");
// 7: 한글 + 영문 + 숫자
$pattern = '/[\x{1100}-\x{11FF}\x{3130}-\x{318F}\x{AC00}-\x{D7AF}0-9a-zA-Z]+/u';
preg_match_all($pattern,$convMsg,$match);
$resultArr[7] = mb_convert_encoding(implode('',$match[0]),"EUC-KR", "UTF-8");
// 11: 한글 + 영어 + 특수기호
$pattern = '/[^0-9]/';
preg_match_all($pattern,$convMsg,$match);
$resultArr[11] = mb_convert_encoding(implode('',$match[0]),"EUC-KR", "UTF-8");
// 14: 영문 + 숫자 + 특수기호
$pattern = '/[^\x{1100}-\x{11FF}\x{3130}-\x{318F}\x{AC00}-\x{D7AF}]+/u';
preg_match_all($pattern,$convMsg,$match);
$resultArr[14] = mb_convert_encoding(implode('',$match[0]),"EUC-KR", "UTF-8");
// 13: 한글 + 숫자 + 특수기호
$pattern = '/[^a-zA-Z]/';
preg_match_all($pattern,$convMsg,$match);
$resultArr[13] = mb_convert_encoding(implode('',$match[0]),"EUC-KR", "UTF-8");
// 15: 한글 + 영문 + 숫자 + 특수기호
$resultArr[15] = $convMsg;
return $resultArr;
}
'PHP∵SCRIPT' 카테고리의 다른 글
PHP에서 특수문자 없애는 정규표현식 (0) | 2013.12.31 |
---|---|
정규식 특수문자 PHP (0) | 2013.12.31 |
정규식, 특수문자 입력 제한 (0) | 2013.12.31 |
preg_match 검색대상에 엔터가 들어간경우는 검색옵션에 s 를 추가하라. (0) | 2013.12.31 |
영문자로만 되어 있는지 (0) | 2013.12.31 |
문자열에서 영문+숫자만추출하기 (0) | 2013.12.31 |
번호 시작 1. tets 2.tets 11.test 추출 정렬 처리 (0) | 2013.12.31 |
내용중 image 추출 처리 (0) | 2013.12.31 |
댓글