PHP Velho Oeste 2024

mb_chr

(PHP 7 >= 7.2.0, PHP 8)

mb_chr按 Unicode 码位值返回字符

说明

mb_chr(int $codepoint, ?string $encoding = null): string|false

返回一个含有由 Unicode 码位值所指定的字符的字符串,以指定的编码进行编码。

此函数与 mb_ord() 互补。

参数

codepoint

一个 Unicode 码位值,例如 128024U+1F418 ELEPHANT

encoding

encoding 参数为字符编码。如果省略或是 null,则使用内部字符编码。

返回值

如果可以用所指定的编码表示,则返回含有所请求的字符的字符串, 或者在失败时返回 false

更新日志

版本 说明
8.0.0 现在 encoding 可以为 null。

示例

示例 #1 Testing different code points

<?php
$values
= [65, 63, 0x20AC, 128024];
foreach (
$values as $value) {
var_dump(mb_chr($value, 'UTF-8'));
var_dump(mb_chr($value, 'ISO-8859-1'));
}
?>

以上示例会输出:

string(1) "A"
string(1) "A"
string(1) "?"
string(1) "?"
string(3) "€"
bool(false)
string(4) "🐘"
bool(false)

参见

add a note add a note

User Contributed Notes 1 note

up
0
boss3333 at laposte dot net
5 years ago
To convert the utf16 or utf8 decimal values (utf-8 can translate both encodings) to ascii characters using mb_ord, follow these steps:

Starting value:

1_Zvpxs4pf

Value converted to decimal value utf-16 or utf-8 (see ascii coding table):

49,95,90,118,112,120,115,52,112,102

recovery with mb_ord:

use: $ char = mb_chr ($ decimalValue, 'UTF-8');
return the correct character;

1, _, Z, v, p, x, s, 4, p, f

example:
$ start_String = 1_Zvpxs4pf;
$ start_String_to_Decimal = 49959011811212011552112102;
(decimal value of each character add in an array);
$ tab = [49, 95, 90, 118, 112, 120, 115, 52, 112, 102];
$ size = sizeof ($ tab);

$ tmpStr = '';

for ($ i = 0; $ i <$ size; $ i ++)
{
$ tmpStr. = mb_chr ($ tab [$ i], 'UTF-8');
}

echo $ tmpStr;

1_Zvpxs4pf
To Top