Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

substr with japanese characters issue

i'm echoing japanese characters fine but when i try to substr and echo out part of the string it just turn to question marks ���

note: i set my header to utf-8

header('Content-Type: text/html; charset=utf-8');

and made the meta <meta http-equiv="Content-type" content="text/html; charset=utf-8" />

$word = "せんせい";
echo $word;       //works just fine

echo substr($word,-1);    //now it just echoes �

//this one also failed
echo $word[0];    //echoes �
like image 399
Mohamed El-Shafey Avatar asked Jul 31 '12 10:07

Mohamed El-Shafey


3 Answers

mb_substr

will work. But, remember to add the following line at the top of your script:

mb_internal_encoding("UTF-8");//Sets the internal character encoding to UTF-8, for mb_substr to work
like image 195
oabarca Avatar answered Oct 11 '22 06:10

oabarca


When working with your multibyte strings, you'll need to use the multibyte string functions, in this case mb_substr.

like image 33
Michael Robinson Avatar answered Oct 11 '22 08:10

Michael Robinson


Try multibyte substrings, mb_substr() info found here This function is made for characters not in the english ascii code set.

like image 31
Branden S. Smith Avatar answered Oct 11 '22 06:10

Branden S. Smith