mb_substr
(PHP 4 >= 4.0.6, PHP 5, PHP 7)
mb_substr — Возвращает часть строки
Описание
$str
, int $start
[, int $length
= NULL
[, string $encoding
= mb_internal_encoding()
]] )
Корректно выполняет substr() для многобайтовых кодировок,
учитывая количество символов. Позиция отсчитывается от начала
str
. Позиция первого символа - 0, второго - 1 и т.д.
Список параметров
-
str
-
Исходная строка (string) для получения подстроки.
-
start
-
Позиция символа
str
, с которой выделяется подстрока. -
length
-
Максимальное количество символов возвращаемой подстроки (string). если не указан, то передается NULL, и извлекаются все символы до конца строки.
-
encoding
-
Параметр
encoding
представляет собой символьную кодировку. Если он опущен, вместо него будет использовано значение внутренней кодировки.
Возвращаемые значения
mb_substr() Возвращает не более length
символов, начиная с позиции start
исходной строки.
str
Смотрите также
- mb_strcut() - Получение части строки
- mb_internal_encoding() - Установка/получение внутренней кодировки скрипта
- PHP Руководство
- Функции по категориям
- Индекс функций
- Справочник функций
- Поддержка языков и кодировок
- Многобайтные строки
- mb_check_encoding
- mb_convert_case
- mb_convert_encoding
- mb_convert_kana
- mb_convert_variables
- mb_decode_mimeheader
- mb_decode_numericentity
- mb_detect_encoding
- mb_detect_order
- mb_encode_mimeheader
- mb_encode_numericentity
- mb_encoding_aliases
- mb_ereg_match
- mb_ereg_replace_callback
- mb_ereg_replace
- mb_ereg_search_getpos
- mb_ereg_search_getregs
- mb_ereg_search_init
- mb_ereg_search_pos
- mb_ereg_search_regs
- mb_ereg_search_setpos
- mb_ereg_search
- mb_ereg
- mb_eregi_replace
- mb_eregi
- mb_get_info
- mb_http_input
- mb_http_output
- mb_internal_encoding
- mb_language
- mb_list_encodings
- mb_output_handler
- mb_parse_str
- mb_preferred_mime_name
- mb_regex_encoding
- mb_regex_set_options
- mb_send_mail
- mb_split
- mb_strcut
- mb_strimwidth
- mb_stripos
- mb_stristr
- mb_strlen
- mb_strpos
- mb_strrchr
- mb_strrichr
- mb_strripos
- mb_strrpos
- mb_strstr
- mb_strtolower
- mb_strtoupper
- mb_strwidth
- mb_substitute_character
- mb_substr_count
- mb_substr
Коментарии
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.
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
I'm trying to capitalize only the first character of the string and tried some of the examples above but they didn't work. It seems mb_substr() cannot calculate the length of the string in multi-byte encoding (UTF-8) and it should be set explicitly. Here is the corrected version:
<?php
function mb_ucfirst($str, $enc = 'utf-8') {
return mb_strtoupper(mb_substr($str, 0, 1, $enc), $enc).mb_substr($str, 1, mb_strlen($str, $enc), $enc);
}
?>
cheers!
quick and dirty loop through multibyte string
<?php
function get_character_classes($string, $encoding = "UTF-8") {
$current_encoding = mb_internal_encoding();
mb_internal_encoding($encoding);
$has = array();
$stringlength = mb_strlen($string, $encoding);
for ($i=0; $i < $stringlength; $i++) {
$c = mb_substr($string, $i, 1);
if (($c >= "0") && ($c <= "9")) {
$has['numeric'] = "numeric";
} else if (($c >= "a") && ($c <= "z")) {
$has['alpha'] = "alpha";
$has['alphalower'] = 'alphalower';
} else if (($c >= "A") && ($c <= "Z")) {
$has['alpha'] = "alpha";
$has['alphaupper'] = "alphaupper";
} else if (($c == "$") || ($c == "£")) {
$has['currency'] = "currency";
} else if (($c == ".") && ($has['decimal'])) {
$has['decimals'] = "decimals";
} else if ($c == ".") {
$has['decimal'] = "decimal";
} else if ($c == ",") {
$has['comma'] = "comma";
} else if ($c == "-") {
$has['dash'] = "dash";
} else if ($c == " ") {
$has['space'] = "space";
} else if ($c == "/") {
$has['slash'] = "slash";
} else if ($c == ":") {
$has['colon'] = "colon";
} else if (($c >= " ") && ($c <= "~")) {
$has['ascii'] = "ascii";
} else {
$has['binary'] = "binary";
}
}
mb_internal_encoding($current_encoding);
return $has;
}
$string = "1234asdfA£^_{}|}~žščř";
echo print_r(get_character_classes($string), true);
?>
Array
(
[numeric] => numeric
[alpha] => alpha
[alphalower] => alphalower
[alphaupper] => alphaupper
[currency] => currency
[ascii] => ascii
[binary] => binary
)
A serious pitfall when using mb_substr() set to HTML-ENTITIES encoding is that the function performs a number of conversions before returning the value, the worst one being that html special characters are not just counted but decoded.
<?php
mb_internal_encoding("ISO-8859-1"); echo mb_internal_encoding(),"\n<br><br>\n";
$a='jüst ä " simple " 日本 <b>test</b>';
echo mb_substr($a,0),"\n<br><br>\n";
// page source: jüst ä " simple " 日本 <b>test</b>
echo mb_substr($a,0,strlen($a),'HTML-ENTITIES');
// page source: jüst ä " simple " 日本 <b>test</b>
?>
As you often need to iterate over UTF-8 characters inside a string, you might be tempted to use mb_substr($text,$i,1).
The problem with this is that there is no "magic" way to find $i-th character inside UTF-8 string, other than reading it byte by byte from the begining. Thus a loop which calls mb_substr($text,$i,1) N times for all possible N values of $i, will take much longer than expected. The larger the $i gets, the longer is the search for $i-th letter. As characters are between 1 to 6 bytes long, one can convince oneself, that the execution time of such loop is actually Theta(N^2), which can be really slow even for moderately long texts.
One way to work around it is to first split your text into an array of letters using some smart preprocessing, and only then iterate over the array.
Here is the idea:
<?php
class Strings
{
public static function len($a){
return mb_strlen($a,'UTF-8');
}
public static function charAt($a,$i){
return self::substr($a,$i,1);
}
public static function substr($a,$x,$y=null){
if($y===NULL){
$y=self::len($a);
}
return mb_substr($a,$x,$y,'UTF-8');
}
public static function letters($a){
$len = self::len($a);
if($len==0){
return array();
}else if($len == 1){
return array($a);
}else{
return Arrays::concat(
self::letters(self::substr($a,0,$len>>1)),
self::letters(self::substr($a,$len>>1))
);
}
}
?>
As you can see, the Strings::letters($text) split the text recursively into two parts. Each level of the recursion requires time linear in the length of the string, and there is logarithmic number of levels, so the total runtime is O(N log N), which is still more than theoretically optimal O(N), but sadly this is the best idea I've got.
you can make mb_substr working faster with long strings with usage of ucs-2 encoding.
<?php
header('Content-Type: text/html; charset=utf-8');
echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" >';
function test($string, $encoding='utf8'){
$t1=microtime(true);
$textlen=mb_strlen($string);
$substr_len=3;
for($i=0;$i<$textlen-$substr_len+1;$i++){
$substr=mb_substr($string,$i,$substr_len);
}
echo 'mb_substr, '.$encoding.': '.(microtime(true)-$t1);
echo ' . check: ';
if($encoding=='ucs2'){
$substr=mb_convert_encoding($substr,'utf-8','ucs2');
}
var_dump( $substr );
echo ' . <br>';
echo '<br>';
}
$corpus_short=str_repeat('тест Тест ',1000);
// it works likewise slowly with "test Test" with utf8
mb_internal_encoding('utf-8');
test($corpus_short);
$corpus_short_ucs2=mb_convert_encoding($corpus_short,'ucs2','utf-8');
mb_internal_encoding('ucs2');
test($corpus_short_ucs2,'ucs2');
?>
output:
mb_substr, utf8: 0.26480984687805 . check: string(5) "ст " .
mb_substr, ucs2: 0.0048871040344238 . check: string(5) "ст " .
Here is an example demonstrating the difference between `substr` and `mb_substr` functions:
1- When working with non UTF-8 characters, both functions behave the same and give the same output:
$str = 'abcdef';
echo substr($s, 0, 3); // abc
echo mb_substr($s, 0, 3); // abc
2- When working with UTF-8 characters, each function will behave differently and give a different result:
2.A- The 'substr' function works on the byte-level and with single-byte encoded characters only (doesn't support multibyte encoding).
For example:
$str_utf8 = utf8_encode("déjà_vu");
If we do this:
echo substr($str_utf8, 0, 3); // dé
echo substr($str_utf8, 0, 2); // d�
=> That's because the special character "é" (and "à") is internally coded with two bytes:
PHP will start reading the first byte at index 0, which represents `d`, then move to the second byte, which is a part of the two-byte encoding of the character ` é `, and since the length is set to 2, PHP will stop here and doesn't continue reading the third byte, so it doesn't recognize the character ` é ` and prints � instead of é.
2.B- The 'mb_substr' function works on the character-level and supports multibyte encoded characters. This means, PHP counts the number of characters only and doesn't take into consideration the number of bytes of their encoding, for example:
$str_utf8 = utf8_encode("déjà_vu");
echo mb_substr($str_utf8, 0, 4, "UTF-8"); // Déjà
echo mb_substr($str_utf8, 1, 4, "UTF-8"); // éjà_
echo mb_substr($str_utf8, 6, 4, "UTF-8"); // u
echo mb_substr($str_utf8, 7, 4, "UTF-8"); // ''
echo mb_substr($str_utf8, -2, "UTF-8"); // vu
echo mb_substr($str_utf8, -2, 1, "UTF-8"); // v
echo mb_substr($str_utf8, -2, 3, "UTF-8"); // vu
Just wanted to add that not only the `start` can be a negative number, the `length` can be negative, too. And it works as expected.
mb_substr( "1234567890", 3, -4, "UTF-8" ) => "456".
So it cuts off the last 4 characters.