Language/JavaScript

Emoji 삭제

idleday 2023. 4. 14. 11:53

⚠️ 이슈

 

Emoji 저장 실패 오류

Emoji 저장 실패 오류 에러 현상 Incorrect string value: '\xF0\x9F\x93\xB201...' for column 'content' 에러 이유 윈도우 이모지(utf8mb4)와 DB(utf8)의 문자 인코딩 설정 상이 MySQL DB collation MariaDB에서 table을 생성할 때 c

idleday.tistory.com

 

해결

  • In detail, this function first uses TextEncoder to convert content into a byte array with utf-8 encoding, then loops through this array, if it finds a byte whose first five bits are 11110 (i.e. 0xF0), it means this is an emoji start, then it replaces this byte and the next three bytes with 0x30 (i.e. number 0). Finally, it uses TextDecoder to convert the modified byte array back to a string, and uses replaceAll method to remove extra 0s.
function removeEmoji (content) {
  let conByte = new TextEncoder("utf-8").encode(content);
  for (var i = 0; i < conByte.length; i++) {
     if ((conByte[i] & 0xF8) == 0xF0) {
         for (var j = 0; j < 4; j++) {
             conByte[i+j]=0x30;
         }
         i += 3;
     }
  }
  content = new TextDecoder("utf-8").decode(conByte);
  return content.replaceAll("0000", "");
}

 

 


Ref

 

How to remove emoji code using javascript?

How do I remove emoji code using JavaScript? I thought I had taken care of it using the code below, but I still have characters like 🔴. function removeInvalidChars() { return this.replace(/[\...

stackoverflow.com

 

'Language > JavaScript' 카테고리의 다른 글

[JS] 문자열 자르기  (0) 2023.11.08
자바스크립트 람다식(화살표 함수)  (0) 2023.04.19
Broadcast Channel API  (0) 2023.04.19
formData 키값 출력  (0) 2023.04.12
for / for in / for of  (2) 2022.12.15