PHP Velho Oeste 2024

mb_ord

(PHP 7 >= 7.2.0, PHP 8)

mb_ordGet Unicode code point of character

Description

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

Returns the Unicode code point value of the given character.

This function complements mb_chr().

Parameters

string

A string

encoding

The encoding parameter is the character encoding. If it is omitted or null, the internal character encoding value will be used.

Return Values

The Unicode code point for the first character of string or false on failure.

Changelog

Version Description
8.0.0 encoding is nullable now.

Examples

<?php
var_dump
(mb_ord("A", "UTF-8"));
var_dump(mb_ord("🐘", "UTF-8"));
var_dump(mb_ord("\x80", "ISO-8859-1"));
var_dump(mb_ord("\x80", "Windows-1252"));
?>

The above example will output:


int(65)
int(128024)
int(128)
int(8364)

See Also

  • mb_internal_encoding() - Set/Get internal character encoding
  • mb_chr() - Return character by Unicode code point value
  • IntlChar::ord() - Return Unicode code point value of character
  • ord() - Convert the first byte of a string to a value between 0 and 255

add a note

User Contributed Notes 1 note

up
4
Andrew
3 years ago
You can forget about DIY uniord()
https://www.php.net/manual/en/function.ord.php#42778

$array['Б'] = uniord('Б');
$array['🚷'] = uniord('🚷');
$array['mb_ord Б'] = mb_ord('Б');
$array['mb_ord 🚷'] = mb_ord('🚷');

function uniord($charUTF8)
{
$charUCS4 = mb_convert_encoding($charUTF8, 'UCS-4BE', 'UTF-8');
$byte1 = ord(substr($charUCS4, 0, 1));
$byte2 = ord(substr($charUCS4, 1, 1));
$byte3 = ord(substr($charUCS4, 2, 1));
$byte4 = ord(substr($charUCS4, 3, 1));
return ($byte1 << 32) + ($byte2 << 16) + ($byte3 << 8) + $byte4;
}

var_export($array);

Shows:

array ( 'Б' => 1041, '🚷' => 128695, 'mb_ord Б' => 1041, 'mb_ord 🚷' => 128695, )

https://unicode-table.com/en/0411/
Б
Encoding hex dec (bytes) dec binary
UTF-8 D0 91 208 145 53393 11010000 10010001
UTF-16BE 04 11 4 17 1041 00000100 00010001
UTF-16LE 11 04 17 4 4356 00010001 00000100
UTF-32BE 00 00 04 11 0 0 4 17 1041 00000000 00000000 00000100 00010001
UTF-32LE 11 04 00 00 17 4 0 0 285474816 00010001 00000100 00000000 00000000

https://unicode-table.com/en/1F6B7/
🚷
Encoding hex dec (bytes) dec binary
UTF-8 F0 9F 9A B7 240 159 154 183 4036991671 11110000 10011111 10011010 10110111
UTF-16BE D8 3D DE B7 216 61 222 183 3627933367 11011000 00111101 11011110 10110111
UTF-16LE 3D D8 B7 DE 61 216 183 222 1037613022 00111101 11011000 10110111 11011110
UTF-32BE 00 01 F6 B7 0 1 246 183 128695 00000000 00000001 11110110 10110111
UTF-32LE B7 F6 01 00 183 246 1 0 3086352640 10110111 11110110 00000001 00000000
To Top