N
N
Nahs2016-05-17 14:49:52
PHP
Nahs, 2016-05-17 14:49:52

How to convert utf-8 letter to html code (A -> А)?

There is a character base in the html code.
There is a need to compare the input character with the character code from the database.
How can this be implemented?

// Это не отрабатывает как ожидается
echo htmlsprcialchars(htmlentity('A')); //&#1040

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
kpa6uu, 2016-05-17
@Nahs

function encode($str) {
    $str = mb_convert_encoding($str , 'UTF-32', 'UTF-8');
    $split = str_split($str, 4);

    $res = "";
    foreach ($split as $c) {
        $cur = 0;
        for ($i = 0; $i < 4; $i++) {
            $cur |= ord($c[$i]) << (8*(3 - $i));
        }
        $res .= "&#" . $cur . ";";
    }
    return $res;
}

var_dump(encode('t'));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question