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
$word = "せんせい";
echo $word; //works just fine
echo substr[$word,-1]; //now it just echoes �
//this one also failed
echo $word[0]; //echoes �
asked Jul 31, 2012 at 10:17
1
When working with your multibyte strings, you'll need to use the multibyte string
functions, in this case mb_substr
.
answered Jul 31, 2012 at 10:18
Michael RobinsonMichael Robinson
28.7k12 gold badges103 silver badges129 bronze badges
2
Try multibyte substrings, mb_substr[] info found here This function is made for characters not in the english ascii code set.
answered Jul 31, 2012 at 10:19
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
answered Jan 7, 2015 at 5:10
oabarcaoabarca
10.4k6 gold badges56 silver badges70 bronze badges
Not the answer you're looking for? Browse other questions tagged php utf-8 character-encoding or ask your own question.
[PHP 4 >= 4.0.6, PHP 5, PHP 7, PHP 8]
mb_substr — Get part of string
Description
mb_substr[
string $string
,
int $start
,
?int
$length
= null
,
?string $encoding
= null
]: string
Parameters
string
The string to extract the substring from.
start
If start
is non-negative, the returned string will start at the
start
'th position in string
, counting from zero. For instance, in the string 'abcdef
', the character at position 0
is 'a
', the character at position 2
is 'c
', and so forth.
If start
is negative, the returned string will start at the start
'th character from the end of string
.
length
Maximum number of characters to use from string
. If omitted or NULL
is passed, extract all characters to the end of the
string.
encoding
The encoding
parameter is the character encoding. If it is omitted or null
, the internal character encoding value will be used.
Return Values
mb_substr[] returns the portion of string
specified by the start
and length
parameters.
Changelog
8.0.0 | encoding is nullable now.
|
See Also
- mb_strcut[] - Get part of string
- mb_internal_encoding[] - Set/Get internal character encoding
qbolec at gmail dot com ¶
7 years ago
As you often need to iterate over UTF-8 characters inside a string, you might be tempted to use mb_substr[$text,$i,1].
The problem with this is that there is no "magic" way to find $i-th character inside UTF-8 string, other than reading it byte by byte from the begining. Thus a loop which calls mb_substr[$text,$i,1] N times for all possible N values of $i, will take much longer than expected. The larger the $i gets, the longer is the search for $i-th letter. As characters are between 1 to 6 bytes long, one can convince oneself, that the execution time of such loop is actually Theta[N^2], which can be really slow even for moderately long texts.
One way to work around it is to first split your text into an array of letters using some smart preprocessing, and only then iterate over the array.
Here is the idea:
As you can see, the Strings::letters[$text] split the text recursively into two parts. Each level of the recursion requires time linear in the length of the string, and there is logarithmic number of levels, so the total runtime is O[N log N], which is still more than theoretically optimal O[N], but sadly this is the best idea I've got.
xiaogil at yahoo dot fr ¶
17 years ago
Thanks Darien from /freenode #php for the following example [a little bit changed].
It just prints the 6th character of $string.
You can replace the digits by the same in japanese, chinese or whatever language to make a test, it works perfect.
[I couldn't replace 0123456789 by chinese numbers for example here, because it's automatically converted into latin digits on this website, look :
零一二三四
五六七八九]
gilv
drraf at tlen dot pl ¶
17 years ago
Note: If borders are out of string - mb_string[] returns empty _string_, when function substr[] returns _boolean_ false in this case.
Keep this in mind when using "===" comparisions.
Example code:
It's especially confusing when using mbstring with function overloading turned on.
desmatic at gmail dot com ¶
9 years ago
quick and dirty loop through multibyte string
Array
[
[numeric] => numeric
[alpha] => alpha
[alphalower] => alphalower
[alphaupper] => alphaupper
[currency] => currency
[ascii] => ascii
[binary] => binary
]
p dot assenov at aip-solutions dot com ¶
10 years ago
I'm trying to capitalize only the first character of the string and tried some of the examples above but they didn't work. It seems mb_substr[] cannot calculate the length of the string in multi-byte encoding [UTF-8] and it should be set explicitly. Here is the corrected version:
cheers!
qdinar at gmail dot com ¶
6 years ago
you can make mb_substr working faster with long strings with usage of ucs-2 encoding.
mb_substr, utf8: 0.26480984687805 . check: string[5] "ст " . mb_substr, ucs2: 0.0048871040344238 . check: string[5] "ст " .
output:
sanjuro at 1up-games dot com ¶
8 years ago
A serious pitfall when using mb_substr[] set to HTML-ENTITIES encoding is that the function performs a number of conversions before returning the value, the worst one being that html special characters are not just counted but decoded.
projektas at gmail dot com ¶
13 years ago
First letter in upper case