Search code examples
phpencodingutf-8asciifarsi

how getting the persian character ascii code of a string in php


I want to display the character codes of a string in php.

<?php
$Text = '{"text":"سلام"}';
for($i = 0;$i < strlen($Text);$i++){
    echo mb_ord($Text[$i]),"<br>";
}
?>

But its output is null enter image description here


Solution

  • You are trying to get all "bytes" of the unicode string. You need to use mb_strlen() to get the real length of the characters.

    Then, you need to use mb_substr() to get the right character.

    $Text = '{"text":"سلام"}';
    for($i = 0; $i < mb_strlen($Text); $i++) {
        echo mb_ord(mb_substr($Text,$i, 1)), "<br>";
    }
    

    Output :

    123
    34
    116
    101
    120
    116
    34
    58
    34
    1587
    1604
    1575
    1605
    34
    125
    

    See also all mb_* functions