Using PHP to search for a Unicode character

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

I have a sample code in javascript:

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

This returns 72

How to do it in PHP?

+6
source share
4 answers

An equivalent of the above would be

 $str = "HELLO WORLD"; $n = ord($str[0]); 
+2
source

Ascii

This will help:

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

Unicode

But since these functions 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('ب'); 
+5
source

Mark this note in the docs:

http://www.php.net/manual/en/function.ord.php

There is a function here that returns a UTF-8 value, which I assume you need.

+5
source

To be unicode, you must try this (http://us.php.net/manual/en/function.ord.php)

 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; } 
0
source

Source: https://habr.com/ru/post/928181/


All Articles