how to get unicode character from a unicode string in php

前端 未结 2 1605
面向向阳花
面向向阳花 2020-12-21 07:30

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

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

outpu

相关标签:
2条回答
  • 2020-12-21 07:58

    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);
    
    0 讨论(0)
  • 2020-12-21 08:08

    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.

    0 讨论(0)
提交回复
热议问题