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

search for in the

muscat> <Multibyte String
Last updated: Sun, 25 Nov 2007

view this page in

mb_substr

(PHP 4 >= 4.0.6, PHP 5)

mb_substr — Get part of string

Popis

string mb_substr ( string $str , int $start [, int $length [, string $encoding ]] )

mb_substr() returns the portion of str specified by the start and length parameters.

mb_substr() performs 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.

If encoding is omitted, internal encoding is assumed.

encoding is character encoding. If it is omitted, internal character encoding is used.

See also mb_strcut(), mb_internal_encoding().



muscat> <Multibyte String
Last updated: Sun, 25 Nov 2007
 
add a note add a note User Contributed Notes
mb_substr
zachary dot craig at gmail dot com
10-Nov-2008 01:59
@Silvan

Make sure to use encoding with strlen as well. Use:

<?php
mb_substr
($str,$start,mb_strlen($str, $encoding),$encoding);
?>
projektas at gmail dot com
21-Oct-2008 06:29
First letter in upper case <hr />

<?php
header
('Content-type: text/html; charset=utf-8');

if (isset(
$_POST['check']) && !empty($_POST['check'])) {
    echo
htmlspecialchars(ucfirst_utf8($_POST['check']));
} else {
    echo
htmlspecialchars(ucfirst_utf8('Žąsinų'));
}

function
ucfirst_utf8($str) {
    if (
mb_check_encoding($str,'UTF-8')) {
       
$first = mb_substr(
           
mb_strtoupper($str, "utf-8"),0,1,'utf-8'
       
);
        return
$first.mb_substr(
           
mb_strtolower($str,"utf-8"),1,mb_strlen($str),'utf-8'
       
);
    } else {
        return
$str;
    }
}
?>

<form method="post" action="" >
    <input type="input" name="check" />
    <input type="submit" />
</form>
Silvan
01-Sep-2007 03: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 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 :
&#38646;&#19968;&#20108;&#19977;&#22235;
&#20116;&#20845;&#19971;&#20843;&#20061;)

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

muscat> <Multibyte String
Last updated: Sun, 25 Nov 2007
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites