字符编码相关函数使用示例

<?php
echo '当前字符编码:' . mb_internal_encoding() . PHP_EOL; // 当前字符编码:UTF-8


$str = 'hello, world';
echo mb_detect_encoding($str) . PHP_EOL; // ASCII


$str = 'PHP是世界上最好の语言';
echo mb_detect_encoding($str) . PHP_EOL; // UTF-8


$str = '🐵';
echo mb_detect_encoding($str) . PHP_EOL; // UTF-8


//========== 字符编码转换 ==========//
$str = 'PHP是世界上最好の语言';
$str = mb_convert_encoding($str, 'ASCII', 'UTF-8'); // UTF-8转为ASCII
echo mb_detect_encoding($str) . PHP_EOL; // ASCII
echo $str . PHP_EOL; // PHP?????????

Copyright © 2024 码农人生. All Rights Reserved