mb_strimwidth
(PHP 4 >= 4.0.6, PHP 5, PHP 7)
mb_strimwidth — Получение строки, обрезанной до заданной ширины
Описание
$str
, int $start
, int $width
[, string $trimmarker
= ""
[, string $encoding
= mb_internal_encoding()
]] )
Обрезает строку string str
по ширине width
.
Список параметров
-
str
-
Исходная строка string.
-
start
-
Смещение от начала строки. Количество символов от начала строки (Первый символ стоит на позиции 0)
-
width
-
Ожидаемая ширина вырезаемой части.
-
trimmarker
-
Строка, добавляемая в конец обрезанной строки.
-
encoding
-
Параметр
encoding
представляет собой символьную кодировку. Если он опущен, вместо него будет использовано значение внутренней кодировки.
Возвращаемые значения
Обрезанная строка string. Если аргумент
trimmarker
задан,
trimmarker
добавляется к возвращаемому значению.
Примеры
Пример #1 Пример использования mb_strimwidth()
<?php
echo mb_strimwidth("Hello World", 0, 10, "...");
// Выведет Hello W...
?>
Смотрите также
- mb_strwidth() - Возвращает ширину строки
- 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
Коментарии
While having the option to append a string to the end is nice, you can run into the problem of having a space between the append if the width is truncated after a space. I find something like this to be more attractive in the output, although messy in the code.
rtrim(mb_strimwidth($string, 0, 24))."..."
<?php
function strimwidthCenter( $value, $length = 40 ) {
$valueEncoding = mb_detect_encoding( $value, 'auto', true );
if ( $length >= mb_strwidth( $value, $valueEncoding ) ) {
return $value;
}
$limited = '';
$firstWidth = ceil( $length/2 );
$secondStart = mb_strwidth( $value, $valueEncoding ) - ( $length - $firstWidth );
$secondWidth = $length - $firstWidth +1;
$limited = mb_strimwidth( $value, 0, $firstWidth, '...', $valueEncoding ) . mb_substr( $value, $secondStart, $secondWidth, $valueEncoding );
return $limited;
}
?>
I had a problem (as in another comment) with a space being between the trailing dots and the subject being truncated. Here's how I fixed it:
<?php str_replace( " .", ".", mb_strimwidth( $subject, 0, 30, "..." ) ); ?>
Note that the trimmarker removes characters from the end of the string to trim, so
$hello = 'Lorem ipsum dolor sit amet, lorem ipsum';
$trimmarker = '......................';
echo mb_strimwidth($hello, 0, 20, $trimmarker);
will only add the values of the trim marker, as the character number of $trimmarker is longer than the specified value to trim to, so it will return:
......................
It is therefore better to use this:
mb_strimwidth($hello, 0, 20). $trimmarker;
as the trimmarker won't remove characters of the string in order to make space for itself. It will return:
Lorem ipsum dolor si......................