2017-09-28 61 views
-1

我使用此代码,但似乎串无法正确显示(TRA DJI)保持字符串的原始字符编码在PHP

<?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; 
?> 

我什么都试过,但我不能修复它,请帮助。

+0

确保你的php文件是用utf-8编码的(例如使用记事本+ +)然后在页面的开始处(在<?php'标签之前) –

回答

1

使用unicode字符串的长度和字符数是不同的东西。

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

,而不是这个技巧可以工作:

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

但propper办法是看MB_ *函数,而不是依赖于长度为预测的。