最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - In PHP substring return �� these type of character? - Stack Overflow

programmeradmin3浏览0评论

I want to get the first character of a string in PHP. But it returns some unknown character like . Why does that happen?

$text = "अच्छी ाqस्थति में"; // Hindi String 
$char = $text[0];   // $text{0}; also try here .
echo $char;   // output like �

//expected Output अ

//Below code also used
$char = substr($text , 0 , 1);  // Getting same output

If I used javascript I found that I get perfect output:

var text = "अच्छी ाqस्थति में"; // Hindi String 
var char = text.charAt(0);
console.log(char)   // output like अ

Please anyone tell me about that and a solution to this problem? Why these error if charAt & substr work the same way?

I want to get the first character of a string in PHP. But it returns some unknown character like . Why does that happen?

$text = "अच्छी ाqस्थति में"; // Hindi String 
$char = $text[0];   // $text{0}; also try here .
echo $char;   // output like �

//expected Output अ

//Below code also used
$char = substr($text , 0 , 1);  // Getting same output

If I used javascript I found that I get perfect output:

var text = "अच्छी ाqस्थति में"; // Hindi String 
var char = text.charAt(0);
console.log(char)   // output like अ

Please anyone tell me about that and a solution to this problem? Why these error if charAt & substr work the same way?

Share Improve this question edited May 25, 2018 at 12:43 smottt 3,33011 gold badges41 silver badges46 bronze badges asked Dec 15, 2014 at 5:41 Gunjan PatelGunjan Patel 2,3624 gold badges26 silver badges51 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 13

You should use mbstring for unicode strings.

$char = mb_substr($text, 0, 1, 'UTF-8'); // output अ

You can replace 'UTF-8' with any other encoding, if you need it.

PHP does not support unicode by default. Do note that you need mbstring enabled if you want to use this. You can check by checking if extension is loaded:

if (extension_loaded('mbstring')) {
    // mbstring loaded
}
发布评论

评论列表(0)

  1. 暂无评论