Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

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 ? .

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...