PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

Pspell> <Fonctions sur les chaînes de caractères multi-octets
Last updated: Fri, 05 Sep 2008

view this page in

mb_substr

(PHP 4 >= 4.0.6, PHP 5)

mb_substrLit 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.



add a note add a note User Contributed Notes
mb_substr
Silvan
02-Sep-2007 12:30
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);
?>
xiaogil at yahoo dot fr
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 :
&#38646;&#19968;&#20108;&#19977;&#22235;
&#20116;&#20845;&#19971;&#20843;&#20061;)

gilv
drraf at tlen dot pl
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.

 
show source | credits | stats | sitemap | contact | advertising | mirror sites