Passing null as length will not make mb_substr use it's default, instead it will interpret it as 0.
<?php
mb_substr($str,$start,null,$encoding); //Returns '' (empty string) just like substr()
?>
Instead use:
<?php
mb_substr($str,$start,mb_strlen($str),$encoding);
?>
mb_substr
(PHP 4 >= 4.0.6, PHP 5)
mb_substr — 文字列の一部を得る
説明
string mb_substr
( string $str
, int $start
[, int $length
[, string $encoding
]] )
文字数に基づきマルチバイト対応の substr() 処理を行います。位置は、 str の始めから数えられます。 最初の文字の位置は 0、2 番目の文字の位置は 1、といったようになります。
パラメータ
- str
-
調べたい文字列。
- start
-
str の中の使用開始位置。
- length
-
返す文字列の最大の長さ。
- encoding
-
encoding パラメータには文字エンコーディングを指定します。省略した場合は、 内部文字エンコーディングを使用します。
返り値
mb_substr() は、start および length パラメータで指定した str の一部を返します。
mb_substr
Silvan
02-Sep-2007 07:30
02-Sep-2007 07:30
xiaogil at yahoo dot fr
03-Aug-2005 12:33
03-Aug-2005 12:33
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.
<?php
mb_internal_encoding("UTF-8");
$string = "0123456789";
$mystring = mb_substr($string,5,1);
echo $mystring;
?>
(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
23-Feb-2005 11:44
23-Feb-2005 11:44
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:
<?php
var_dump( substr( 'abc', 5, 2 ) ); // returns "false"
var_dump( mb_substr( 'abc', 5, 2 ) ); // returns ""
?>
It's especially confusing when using mbstring with function overloading turned on.
