-1

I uses this code, but it seems substring can't display correctly ( tra đi� )

<?php
    header ('Content-type: text/html; charset=utf-8');
    $msg = $_GET['msg'];
    $command = substr($msg,0,8);
    if (($command == 'tra điểm') or ($command == 'Tra điểm')) {
        $string = array(
            "text" => "done",
        );

        $mjson = json_encode($string);

        echo '{"messages":['.$mjson.']}';
    }
    echo $command;
?>

I tried everything but I can't fix it, please help.

Edwin
  • 1,135
  • 2
  • 16
  • 24
  • Make sure your php file is encoded in utf-8 (usingn Notepad++ for example) And add your header as an html header at the begining of the page (before the ` – m.nachury Sep 28 '17 at 10:28

1 Answers1

1

Using unicode string length and character count are different things.

instead of

$command=substr($msg,0,8);

this hack could work:

$command=substr($msg,0,strlen('Tra điểm'));

But the propper way would be to look at the mb_* functions and not relying on length being predictable.