PHP detect encoding of multibyte characters

This function will test if the characters are UTF-8 or not. You may have to modify the character set to test against in the mb_detect_encoding function to fit your needs.

function mb_str_split( $string ) {
	return preg_split('/(?<!^)(?!$)/u', $string );
$string = "更多學習";
$charlist = mb_str_split($string);
echo '<table>';
foreach($charlist as $char) {
	echo '<tr>';
	echo '<td>' . $char . '</td>';
	echo '<td>' . mb_detect_encoding($char, 'UTF-8', true) . '</td>';
	echo '</tr>';
echo '</table>';

Leave a comment

Your email address will not be published. Required fields are marked *