2

Why

$output = '<hello';
echo mb_convert_encoding( $output, 'UTF-16', 'UTF-8' );

displays

<�hello

I was expecting

<hello

Any ideas?

Edit: Seems related to the browser because it works in command line. But I still don't understand...

Aurel
  • 21
  • 2
  • I can't reproduce the problem but �, U+FFFD is the REPLACEMENT CHARACTER: Replaces an invalid or unrecognizable character. Indicates a Unicode error. Probably the generated UTF-16 isn't being parsed as UTF-16. – Quentin Aug 24 '21 at 09:13
  • @quentin I tried to add `` but I still get `<�hello` (in a Chrome page). You get ` – Aurel Aug 24 '21 at 09:53

1 Answers1

-1
$ echo '<hello' | od -x
0000000      683c    6c65    6f6c    000a                                
0000007
$ php -v
PHP 7.3.11 (cli) (built: Jun  5 2020 23:50:40) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.11, Copyright (c) 1998-2018 Zend Technologies
$ cat hoge.php 
<?php
$output = '<hello';
echo mb_convert_encoding( $output, 'UTF-16', 'UTF-8' );
$ php hoge.php | od -x
0000000      3c00    6800    6500    6c00    6c00    6f00                
0000014

I tried it. Is that right?

iyoda
  • 39
  • 4
  • Yes. So it seems it has something to do with the browser... – Aurel Aug 24 '21 at 10:04
  • https://www.w3.org/International/questions/qa-choosing-encodings I'm wondering what the reason is for using utf-16 instead of utf-8. Is it because the existing code is utf-16? – iyoda Aug 24 '21 at 10:29
  • Absolutely no reason... I'm just trying to understand why I get this � – Aurel Aug 24 '21 at 11:17
  • https://stackoverflow.com/questions/496321/utf-8-utf-16-and-utf-32 You might want to take a look at this. – iyoda Aug 24 '21 at 15:51