How to convert some multibyte characters into its numeric html entity using PHP?

4.1k Views Asked by At

Test string:

$s = "convert this: ";
$s .= "–, —, †, ‡, •, ≤, ≥, μ, ₪, ©, ® y ™, ⅓, ⅔, ⅛, ⅜, ⅝, ⅞, ™, Ω, ℮, ∑, ⌂, ♀, ♂ ";
$s .= "but, not convert ordinary characters to entities";
3

There are 3 best solutions below

1
On BEST ANSWER
$encoded = mb_convert_encoding($s, 'HTML-ENTITIES', 'UTF-8'); 

asssuming your input string is UTF-8, this should encode most everything into numeric entities.

0
On

Well htmlentities doesn't work correctly. Fortunately someone has posted code on the php website that seems to do the translation of multibyte characters properly

0
On

I did work on decoding ascii into html coded text (&#xxxx). https://github.com/hellonearthis/ascii2web