Alternative mb_convert_encoding with HTML-ENTITIES encoding

I have the following code:

mb_convert_encoding($string, 'HTML-ENTITIES', 'utf-8'); 

I need to have alternative code that does the same, but does not use any mb_ * functions (the mb extension is not available in some environments).

I thought that

 utf8_decode(htmlentities($string, ENT_COMPAT, 'utf-8')); 

should do the same, but unfortunately does not.

+8
string php character-encoding
source share
1 answer

I played a little and found it very interesting. It seems that the second part also launches "htmlspecialchars". There should be some error in mb_convert_encoding, since htmlentities is not running correctly.

If you run htmlspecialchars_decode on the result, you will get exactly the same as if you used mb_convert_encoding.

The code:

 $string = 'Test:!"$%&/()=Γ–Γ„ΓœΓΆΓ€ΓΌ<<'; echo mb_convert_encoding($string, 'HTML-ENTITIES', 'utf-8')."\n\n"; echo htmlspecialchars_decode(utf8_decode(htmlentities($string, ENT_COMPAT, 'utf-8', false))); 

Here's a demo of the code: http://sandbox.onlinephpfunctions.com/code/715acade3b8337d9c9e48e58deee2a237015c259

And here is a demo without htmlspecialchars_decode to show your problem: http://sandbox.onlinephpfunctions.com/code/5c4a32bf99aa8fd6246c4a77132a023d32945363

+20
source share

All Articles