4 byte character checking for PHP

Some of the pictograms and Chinese characters are 4bytes of UTF-8 characters, and cannot be saved depending on the version of mysql and character code. I think there is something I want to check that there are more characters than the UTF-8 range in the string.

let’s look at sample, cherry blossom is 4 byte characters.

mb_internal_encoding('UTF-8');
$char = 'abcdefgあいうえお🌸';

echo preg_replace('/[\xF0-\xF7][\x80-\xBF][\x80-\xBF][\x80-\xBF]/', '', $char);

abcdefgあいうえお
Wow, it’s impressive.

Ok then, I want to display an alert if 4-byte characters are included.
Let’s see regular expression below.

mb_internal_encoding('UTF-8');
$char = 'abcdefgあいうえお';
// $char = 'abcdefgあいうえお🌸';

// echo preg_replace('/[\xF0-\xF7][\x80-\xBF][\x80-\xBF][\x80-\xBF]/', '', $char);

if(preg_match('/[\xF0-\xF7][\x80-\xBF][\x80-\xBF][\x80-\xBF]/', $char)){
	echo "alert, it included 4 byte";
} else {
	echo "there is no 4 byte charcter";
}

Oh my goodness.
“just trust yourself. surely, the way to live is coming. Goethe”