Home  >  Article  >  Backend Development  >  How to Retrieve Characters from Unicode Code Points in PHP?

How to Retrieve Characters from Unicode Code Points in PHP?

DDD
DDDOriginal
2024-10-26 10:54:02339browse

How to Retrieve Characters from Unicode Code Points in PHP?

Getting Characters from Unicode Code Points in PHP

PHP offers several functions to manipulate Unicode characters represented by their code points. One common scenario involves retrieving the character associated with a specific Unicode code point.

Solution

PHP provides helper functions to decode HTML entities and convert between UTF-8 and UCS-4BE encodings. Utilizing these functions, we can retrieve characters from Unicode code points as follows:

<code class="php">header('Content-Encoding: UTF-8');

function mb_html_entity_decode($string)
{
    // ... Encoding conversion and decoding logic
}

function mb_ord($string)
{
    // ... UTF-8 to UCS-4BE conversion and unpacking
}

function mb_chr($string)
{
    // ... HTML entity encoding and decoding
}

// Example: Getting the character for U+010F

$codePoint = hexdec('010F');
print mb_chr($codePoint); // Outputs ó</code>

Alternatively:

<code class="php">$codePoint = 243;
print mb_ord('ó'); // Outputs 243
print mb_chr(243); // Outputs ó</code>

The above is the detailed content of How to Retrieve Characters from Unicode Code Points in PHP?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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