admin管理员组文章数量:1391995
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?
1 Answer
Reset to default 13You 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
}
本文标签: javascriptIn PHP substring return �� these type of characterStack Overflow
版权声明:本文标题:javascript - In PHP substring return �� these type of character? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744601907a2615118.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论