Detecting and Removing Emoji Characters in PHP Strings

This article explains how to determine whether a PHP string contains emoji characters, provides functions to detect and strip emojis using multibyte string functions, and discusses storing emoji‑containing strings in MySQL with utf8mb4 or base64 encoding.

php Courses
php Courses
php Courses
Detecting and Removing Emoji Characters in PHP Strings

UTF‑8 encoded emoji characters and some special symbols occupy four bytes, while common Chinese characters use three bytes.

1. Detecting emoji in a string

PHP provides three built‑in functions useful for this task:

mb_strlen – returns the number of characters in a string for a given encoding.

mb_substr – performs a multibyte‑safe substring operation.

strlen – returns the byte length of a string.

mixed mb_strlen(string $str [, string $encoding = mb_internal_encoding()])
string mb_substr(string $str, int $start [, int $length = NULL [, string $encoding = mb_internal_encoding()]])
int strlen(string $string)

Using these functions, the following helper determines if a string contains any emoji:

function haveEmojiChar($str) {
    $mbLen = mb_strlen($str);
    $strArr = [];
    for ($i = 0; $i < $mbLen; $i++) {
        $strArr[] = mb_substr($str, $i, 1, 'utf-8');
        if (strlen($strArr[$i]) >= 4) {
            return true;
        }
    }
    return false;
}

2. Removing emoji from a string

function removeEmojiChar($str) {
    $mbLen = mb_strlen($str);
    $strArr = [];
    for ($i = 0; $i < $mbLen; $i++) {
        $mbSubstr = mb_substr($str, $i, 1, 'utf-8');
        if (strlen($mbSubstr) >= 4) {
            continue;
        }
        $strArr[] = $mbSubstr;
    }
    return implode('', $strArr);
}

3. Storing strings with emoji in MySQL

Use the utf8mb4 character set.

Encode the string with base64_encode before storing and decode after retrieval.

Alternatively, simply remove emoji characters before saving.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

mysqlString Manipulation
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.