Home > Backend Development > PHP Tutorial > How to Convert Unicode Numeric Values to PHP Strings?

How to Convert Unicode Numeric Values to PHP Strings?

Barbara Streisand
Release: 2025-01-02 12:36:40
Original
175 people have browsed it

How to Convert Unicode Numeric Values to PHP Strings?

Converting Unicode Numeric Values to PHP Strings

Question:

How can PHP create strings that contain individual Unicode characters specified by their "Unicode numeric values"?

Answer:

Prior to PHP 7.0.0, this task required the use of the mb_convert_encoding function. However, PHP 7.0.0 introduced the "Unicode codepoint escape" syntax, which allows direct conversion of Unicode numeric values into PHP strings:

$unicodeChar = "\u{1000}";
Copy after login

This notation creates a string containing a single Unicode character whose Unicode numeric value is 1000 (4096 in decimal).

Example:

// PHP 5.x
$unicodeChar = mb_convert_encoding pack('H*', '1000'), 'UTF-8', 'UTF-16BE');

// PHP 7.0+
$unicodeChar = "\u{1000}";

echo "Unicode character: $unicodeChar\n"; // Outputs: "Ѐ"
Copy after login

The above is the detailed content of How to Convert Unicode Numeric Values to PHP Strings?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template