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 — Get part of string
Description
string mb_substr
( string $str
, int $start
[, int $length
[, string $encoding
]] )
Performs a multi-byte safe substr() operation based on number of characters. Position is counted from the beginning of str . First character's position is 0. Second character position is 1, and so on.
Parameters
Return Values
mb_substr() returns the portion of str specified by the start and length parameters.
mb_substr
Silvan
02-Sep-2007 12:30
02-Sep-2007 12:30
xiaogil at yahoo dot fr
02-Aug-2005 05:33
02-Aug-2005 05: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 03:44
23-Feb-2005 03: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.
