Raj
Raj

Reputation: 31

how to get unicode character from a unicode string in php

I want to get a single unicode chatacter from a unicode string.

for example:- $str = "पर्वत निर्माणों में कोनसा संचलन कार्य करता है"; echo $str[0];

output is:- �

but i want to get char 'प' at 0 index of the string.

plz help me how to get char 'प' instead of � .

Upvotes: 3

Views: 1834

Answers (2)

Jukka K. Korpela
Jukka K. Korpela

Reputation: 201588

As @deceze writes, you need to use mb_substr in order to get a character, instead of just a byte. In addition, you need to set the internal encoding with mb_internal_encoding. Assuming that the encoding of your .php file is UTF-8, the following should work:

  mb_internal_encoding('utf-8');
  $str = "पर्वत निर्माणों में कोनसा संचलन कार्य करता है"; 
  echo mb_substr($str, 0, 1);

Upvotes: 6

deceze
deceze

Reputation: 522081

PHP's default $str[x] notation operates on bytes, so you're just getting the first part of a multibyte character. To extract entire encoding aware byte sequences for whole characters, you need to use mb_substr.

Also see What Every Programmer Absolutely, Positively Needs To Know About Encodings And Character Sets To Work With Text.

Upvotes: 1

Related Questions