Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using PHP to find unicode of a character [duplicate]

Tags:

php

Possible Duplicate:
How to get code point number for a given character in a utf-8 string?

I have a sample code in javascript:

var str = "HELLO WORLD";
var n = str.charCodeAt(0);

This returns 72

How do I make this done in PHP?

like image 849
LIGHT Avatar asked Oct 20 '12 15:10

LIGHT


People also ask

How do I find Unicode for a character?

To insert a Unicode character, type the character code, press ALT, and then press X. For example, to type a dollar symbol ($), type 0024, press ALT, and then press X. For more Unicode character codes, see Unicode character code charts by script.

Does PHP use Unicode?

PHP does not offer native Unicode support. PHP only supports a 256-character set. However, PHP provides the UTF-8 functions utf8_encode() and utf8_decode() to provide some basic Unicode functionality. See the PHP manual for strings for more details about PHP and Unicode.

What is Ord in PHP?

The ord() function is a inbuilt function in PHP that returns the ASCII value of the first character of a string. This function takes a character string as a parameter and returns the ASCII value of the first character of this string. Parameter: This function accepts a single parameter $string.


1 Answers

ASCII

This will help:

//Code to Character
echo chr(65);

//Character to Code
echo ord('A');

Unicode

But since these function work for ASCII, for Unicode:

function uniord($u) {
    $k = mb_convert_encoding($u, 'UCS-2LE', 'UTF-8');
    $k1 = ord(substr($k, 0, 1));
    $k2 = ord(substr($k, 1, 1));
    return $k2 * 256 + $k1;
}

echo uniord('ب');
like image 73
M. Ahmad Zafar Avatar answered Oct 04 '22 13:10

M. Ahmad Zafar