이모지 제거

2020. 9. 15. 08:25웹 프로그래밍/PHP

$text = "😍안녕하세요 😍 잘지내시죠?💔ㅇㅋ'ㅇㄹ'\"df\"";
$emojiPattern = '/[\\x{10000}-\\x{1FFFF}]/u';
preg_match_all($emojiPattern, $text, $emoji_out);
//print_r($emoji_out);
$text_out = preg_split( $emojiPattern, $text );
//print_r($text_out);
$text = '';
$k = max(sizeof($emoji_out[0]),sizeof($text_out));
for($j=0; $j<$k; $j++){
	if($text_out[$j]=='' && $emoji_out[0][$j]==''){continue;}
	$text .= $text_out[$j];
	//echo htmlEntitiesOrCode($emoji_out[0][$i]);
	$string = $emoji_out[0][$j];
	$result = htmlentities($string, ENT_COMPAT, "UTF-8");

	//if the output is different from input, an entity was returned
	if ($result != $string) {
		$text .= $result;
		continue;
	}

	//get the html code
	$offset = 0;
	$code = ord(substr($string, $offset,1));
	if($code == 0){continue;}
	if ($code >= 128) {
		if ($code < 224) {
			$bytesnumber = 2;
		} else if ($code < 240) {
			$bytesnumber = 3;
		} else if ($code < 248) {
			$bytesnumber = 4;
		}
		$codetemp = $code - 192 - ($bytesnumber > 2 ? 32 : 0) - ($bytesnumber > 3 ? 16 : 0);
		for ($i = 2; $i <= $bytesnumber; $i++) {
			$offset ++;
			$code2 = ord(substr($string, $offset, 1)) - 128;
			$codetemp = $codetemp*64 + $code2;
		}
		$code = $codetemp;
	}
	$offset += 1;
	if ($offset >= strlen($string)) {
		$offset = -1;
	}
	$text .= "&#" . $code;
}
$text = addslashes($text);
728x90

'웹 프로그래밍 > PHP' 카테고리의 다른 글

2중배열에서 값있는지 찾기  (0) 2021.08.04
전화번호 출력 정규식  (0) 2021.04.21
DB에 따옴표(' 또는 ") 입력시 치환  (0) 2021.03.26
HTTP -> HTTPS  (0) 2021.03.26
페이지 만들기  (0) 2021.03.26