php - 使用php将数字转换为utf-8 unicode

标签 php string utf-8

其实我预期的结果是—— 零০ 一姧 两个২ 三个৩ 四 ৪ 五৫ 六姬 七姭 八৮ 九৯

但我得到了- 零 &#ý৯;ý৯;3৭;৬;ý৯;ý৯;3৭;ý৯;3৮;; 一个 &#ý৯;ý৯;3৭;৬;ý৯;ý৯;3৭;ý৯;;
两个 ý৯;ý৯;3৭;৬;
三个 ý৯;3৭;
四个 ý৯;3৮;
五 ý৯;


八৮
九৯ 请帮助我。 代码:

$n=array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9');

 $x=array("০", "১", "২", "৩", "৪", "৫", "৬", "৭", "৮", "৯");

$on='O 0
 one 1
 two 2
 three 3
 four 4
 five 5
 six 6
 seven 7
 eight 8
 nine 9';

 $converted = nl2br(str_replace($n, $x, $on));


 echo $converted;

最佳答案

str_replace 不是编码安全的。您有一个多字节 str_replace 的实现 ( mb_str_replace ) here :

function mb_str_replace($needle, $replacement, $haystack)
{
    $needle_len = mb_strlen($needle);
    $replacement_len = mb_strlen($replacement);
    $pos = mb_strpos($haystack, $needle);
    while ($pos !== false)
    {
        $haystack = mb_substr($haystack, 0, $pos) . $replacement
                . mb_substr($haystack, $pos + $needle_len);
        $pos = mb_strpos($haystack, $needle, $pos + $replacement_len);
    }
    return $haystack;
}

编辑:哦,您的字符是 HTML 编码的,而不是 PHP 编码问题。

关于php - 使用php将数字转换为utf-8 unicode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14797554/

相关文章:

c - 将字符串分配给c中的结构变量

python-3.x - python3 : UTF-8 encoding in http. 服务器

php登录后导航到上一页解决方法

php - 如何在 PHP 类型提示中捕获 "catchable fatal error"?

python - 在 Python 字符串切片中哪个优先?索引还是步骤?

c - 读取文本的第一行并将其转换为 int,但返回另一个值 [C]

android - 在android中将非英语数据存储和检索到sqlite数据库

javascript - 通过 XMLHTTPRequest 发布时无法设置自定义编码

php - 组合两个表不同的标准

php - 使用 like 子句执行准备好的 PDO 语句