사이트 내 전체검색
php 정규식 종류별 문자 추출
로빈아빠
https://cmd.kr/javascript/840 URL이 복사되었습니다.

본문

Written by Inshane

불펌은 상관하지 않으나 내용 출처는 밝혀주시기 바랍니다. - CEnA

  

홍길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;
  }

댓글목록

등록된 댓글이 없습니다.

831 (1/17P)

Search

Copyright © Cmd 명령어 18.221.24.133