In PHP does substring return this type of character?
I want to get the first character of a string in PHP. But gives an unknown character like. why not?
$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
But if i used in javascript i found the perfect way out.
var text = "अच्छी ाqस्थति में"; // Hindi String
var char = text.charAt(0);
console.log(char) // output like अ
Please, did anyone tell me about this and the solution to this problem? why are these errors if charAt and substr work the same?
+3
source to share
1 answer
You have to 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. Note that you need to include mbstring if you want to use this. You can check if the extension is loaded:
if (extension_loaded('mbstring')) {
// mbstring loaded
}
+12
source to share