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 — Lit une sous-chaîne
Description
string mb_substr
( string $str
, int $start
[, int $length
[, string $encoding
]] )
Effectue une recherche de type strpos(), en tenant compte des caractères multi-octets. La position de needle est comptée à partir du début de la chaîne haystack : les positions commencent à 0.
Liste de paramètres
- str
-
La chaîne à analyser.
- start
-
La première position utilisée dans str .
- length
-
La taille maximale de la chaîne retournée.
- encoding
-
Le paramètre encoding est l'encodage des caractères. S'il est omis, l'encodage de caractres interne sera utilisé.
Valeurs de retour
mb_substr() retourne la portion de la chaîne str qui commence au caractère start et a la longueur de length caractères.
mb_substr
Silvan
01-Sep-2007 03:30
01-Sep-2007 03:30
xiaogil at yahoo dot fr
02-Aug-2005 08:33
02-Aug-2005 08: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 06:44
23-Feb-2005 06: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.
