开发者

Encode any character to numbers/Latin and decode it back

开发者 https://www.devze.com 2023-02-19 21:29 出处:网络
I have a UTF8 string that contains non-English symbols. I 开发者_JS百科need to convert them to Latin/numbers and then get them back.

I have a UTF8 string that contains non-English symbols. I 开发者_JS百科need to convert them to Latin/numbers and then get them back.

I tried htmlentities('字')

The result is å­�, but I need something like x3445 so I can use html_entity_decode() or something similar that will return the original character.

How can I do this with PHP?


Unfortunately, htmlentities will only encode characters that have a named entity. To convert everything else to a numeric entity, you can use mb_encode_numericentities. For example,

$string = mb_encode_numericentity(htmlentities($string, ENT_QUOTES, 'UTF-8'), array (0x80, 0xffff, 0, 0xffff), 'UTF-8');


The third parameter of the htmlentities function allows you to set the charset that you would like to use for the conversion. See http://au.php.net/manual/en/function.htmlentities.php

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号