Current Location: Home> Function Categories> mb_ord

mb_ord

Get the code point of the character
Name:mb_ord
Category:Multi-byte string
Programming Language:php
One-line Description:Returns the Unicode code point value of the first character of the specified string (code point)

Function name: mb_ord()

Applicable version: PHP 7 >= 7.2.0, PHP 8

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

Description: The mb_ord() function returns the Unicode code point value (code point) of the first character of the specified string. It can handle multibyte characters and supports various encodings.

parameter:

  • $string: A string to get the Unicode code point value.
  • $encoding (optional): Encoding of the string, default to internal character encoding. You can use the mb_list_encodings() function to view supported encodings.

Return value:

  • Returns the Unicode code point value (integer) of the first character of the specified string.
  • If the string is empty, false is returned.
  • If the specified encoding is not supported, false is returned.

Example:

 // 示例1 $string = "你好"; $unicode = mb_ord($string, 'UTF-8'); echo $unicode; // 输出:20320 // 示例2 $string = "hello"; $unicode = mb_ord($string); echo $unicode; // 输出:104

Notes:

  • The mb_ord() function only returns the Unicode code point value of the first character of the string. If you need to obtain the Unicode code point value of the entire string, you can use the mb_convert_encoding() function to convert the string to UTF-32 encoding, and then use the unpack() function to unpack and obtain the code point value of each character.
  • If you want to handle ASCII characters, you can use the ord() function, which is more efficient.
Similar Functions
Popular Articles