In PHP substring return �� these type of character?

Gunjan Patel

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?

smottt

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
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

PHP 7 interfaces, return type hinting and self

From Dev

Bash: replace substring between quotes that contains any character type

From Dev

How to find the index of first occurrence of a specific type of character after some substring?

From Dev

Swift: Substring based on a character

From Dev

Substring from character to character in C#

From Dev

Typecasting the return type of iterator.next() method to Character class

From Dev

static return type in PHP 7 interfaces

From Dev

Cannot convert return expression of type Object<String> to return type Object<Character>

From Dev

Return index of longest substring

From Dev

Regexp: substring not followed by character

From Dev

get type in php return "object" and not the object type

From Dev

php7 void return type not working?

From Dev

find the position of character in a substring

From Dev

PHP: Is there a way to get the return type of a method?

From Dev

Is there any way to read character type in PHP?

From Dev

PHP interfaces. Different return type

From Dev

Why int type method is allowing character type as return value?

From Dev

In PHP substring return �� these type of character?

From Dev

PHP: What variable type does 'return;' return?

From Dev

PHP Function with array return type

From Dev

Return substring in array with regex

From Dev

Remove substring like this+immediate next character in php

From Dev

return substring within an array

From Dev

Substring character string based on a certain character in string

From Dev

PHP use regex to return substring

From Dev

How to substring within the special character using PHP?

From Dev

All substring with 'a' character at the end

From Dev

return substring of a dynamic ping

From Dev

How to get substring of string using starting and ending character in PHP?

Related Related

  1. 1

    PHP 7 interfaces, return type hinting and self

  2. 2

    Bash: replace substring between quotes that contains any character type

  3. 3

    How to find the index of first occurrence of a specific type of character after some substring?

  4. 4

    Swift: Substring based on a character

  5. 5

    Substring from character to character in C#

  6. 6

    Typecasting the return type of iterator.next() method to Character class

  7. 7

    static return type in PHP 7 interfaces

  8. 8

    Cannot convert return expression of type Object<String> to return type Object<Character>

  9. 9

    Return index of longest substring

  10. 10

    Regexp: substring not followed by character

  11. 11

    get type in php return "object" and not the object type

  12. 12

    php7 void return type not working?

  13. 13

    find the position of character in a substring

  14. 14

    PHP: Is there a way to get the return type of a method?

  15. 15

    Is there any way to read character type in PHP?

  16. 16

    PHP interfaces. Different return type

  17. 17

    Why int type method is allowing character type as return value?

  18. 18

    In PHP substring return �� these type of character?

  19. 19

    PHP: What variable type does 'return;' return?

  20. 20

    PHP Function with array return type

  21. 21

    Return substring in array with regex

  22. 22

    Remove substring like this+immediate next character in php

  23. 23

    return substring within an array

  24. 24

    Substring character string based on a certain character in string

  25. 25

    PHP use regex to return substring

  26. 26

    How to substring within the special character using PHP?

  27. 27

    All substring with 'a' character at the end

  28. 28

    return substring of a dynamic ping

  29. 29

    How to get substring of string using starting and ending character in PHP?

HotTag

Archive