Mohamed El-Shafey
Mohamed El-Shafey

Reputation: 111

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 �

Upvotes: 9

Views: 5078

Answers (3)

dev4life
dev4life

Reputation: 11394

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

Upvotes: 2

Michael Robinson
Michael Robinson

Reputation: 29498

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

Upvotes: 10

Branden S. Smith
Branden S. Smith

Reputation: 1161

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

Upvotes: 3

Related Questions