Как узнать длину строки php
Перейти к содержимому

Как узнать длину строки php

Как узнать длину строки php

Строки играют большую роль, задачи на парсинг строк довольно часто встречаются в PHP , поэтому рассмотрим некоторые базовые функции работы со строками.

Строки представляют некоторый текст, который заключен в одинарные или двойные кавычки:

Но также PHP позволяет определить строку и без кавычек с помощью оператора <<< , за которым следует метка завершения строки:

В данном случае «LABEL» — это название метки, которая указывает на начало и конец строки. Название метки произвольное. То есть все, что расположено между <<< LABEL и LABEL; , будет представлять сроку $s . В частности, браузер выведет нам следующее:

И в данном случае, как и в обычной строке мы можем использовать переменные:

Обращение к символам строки

PHP позволяет обращаться к отдельным символам строки как к элементам массива по числовому индексу:

Индексация символов, как и в массиве, начинается с нуля.

Специальные функции и расширение mbstring

Кроме некоторых базовых возможностей по работе со строками PHP также предоставляет ряд специальных функций. Для некоторых из них требуется подключить специальное расширение — mbstring.dll . Для подключения этого расширения откроем файл конфигурации PHP — php.ini и найдем в нем следующую строку:

Раскомментируем ее, убрав точку с запятой.

Также убедимся, что у нас указан путь к папке расширений.

Найдем в файле php.ini строку:

Эта строка указывает на каталог с подключаемыми расширениями для PHP. По умолчанию все расширения располагаются в папке ext . Раскомментируем эту строку, убрав точку с запятой, и укажем полный путь к папке ext . Так, в моем случае папка c php расположена в корне диска C (ОС Windows), соответственно я указываю путь «C:\php\ext»:

И так как у нас файл php.ini изменился, перезапустим веб-сервер Apache.

Подключение расширения mbstring позволит задействовать некоторые дополнительные функции для работы со строками, которые в стандартном наборе PHP отсутствуют (такие функции обычно начинаются с префикса mb_ ).

Функции strpos() и mb_strpos()

Функция strpos($str, $search) возвращает позицию подстроки или символа $search в строке $str или значение false , если строка $str не содержит подстроки $search:

При использовании этой функции надо учитывать, что индексация символов в строке начинается с нуля, поэтому позиция символа «T» будет равна 0. Поэтому сравнение $position!=false будет работать некорректно, ведь false и 0 при сравнении и приведении к общему типу будут представлять одно и то же значение. Поэтому в данном случае корректно использовать только операцию эквивалентности: $position!==false или $position===false .

Теперь применим функцию на другом примере:

Неожиданно, но результатом функции будет число 9. Хотя мы видим, что истинная позиция подстроки «мы» в исходной строке равна 5.

Все дело в том, что некоторые строковые функции не всегда корректно обрабатывают кириллические символы, и для них лучше использовать другую функцию — mb_strpos() :

Функция strrpos()

Функция strrpos() во многом аналогична функции strpos() , только ищет позицию не первого, а последнего вхождения подстроки в строку:

Но опять же данная функция не совсем корректно работает с кириллическими символами, поэтому нам надо использовать ее аналог — mb_strrpos() :

Функция trim()

Функция trim($str) удаляет из строки начальные и конечные пробелы, а также управляющие символы \n, \r, \t:

Изменение регистра

Для перевода строки в нижний регистр используется функция strtolower :

Для перевода в нижний регистр строки с кириллическими символами можно использовать функцию mb_strtolower :

Для перевода строки в верхний регистр примеяются функции strtoupper() / mb_ strtoupper() , которые работают аналогично.

Функция strlen()

Функция strlen() возвращает длину строки, то есть количество символов в ней:

Функция strlen() также некорректно работает с кириллицей, поэтому в этом случае лучше применять функцию mb_strlen() :

Получение подстроки

Применяя функцию substr($str, $start [, $length]) , можно получить из одной строки ее определенную часть. Данная функция обрезает строку $str, начиная c символа в позиции $start до конца строки. С помощью дополнительного необязательного параметра $length можно задать количество вырезаемых символов.

Так как данная функция некорректно работает с кириллицей, то вместо нее следует применять функцию mb_substr() , которая действует аналогично:

Замена подстрок

Для замены определенной части строки применяется функция str_replace($old, $new, $input) . Эта функция заменяет в строке $input все вхождения подстроки $old на подстроку $new с учетом регистра:

strlen

Длина строки string в случае успешного выполнения, и 0 , если string пуста.

Примеры

Пример #1 Пример использования strlen()

<?php
$str = ‘abcdef’ ;
echo strlen ( $str ); // 6

$str = ‘ ab cd ‘ ;
echo strlen ( $str ); // 7
?>

Примечания

Замечание:

Функция strlen() возвратит количество байт, а не число символов в строке.

Замечание:

Функция strlen() возвращает null при использовании на массивах, а также выводит ошибку уровня E_WARNING .

Смотрите также

  • count() — Подсчитывает количество элементов массива или Countable объекте
  • grapheme_strlen() — Получает длину строки в единицах графемы
  • iconv_strlen() — Возвращает количество символов в строке
  • mb_strlen() — Получает длину строки

User Contributed Notes 8 notes

I want to share something seriously important for newbies or beginners of PHP who plays with strings of UTF8 encoded characters or the languages like: Arabic, Persian, Pashto, Dari, Chinese (simplified), Chinese (traditional), Japanese, Vietnamese, Urdu, Macedonian, Lithuanian, and etc.
As the manual says: «strlen() returns the number of bytes rather than the number of characters in a string.», so if you want to get the number of characters in a string of UTF8 so use mb_strlen() instead of strlen().

<?php
// the Arabic (Hello) string below is: 59 bytes and 32 characters
$utf8 = «السلام علیکم ورحمة الله وبرکاته!» ;

var_export ( strlen ( $utf8 ) ); // 59
echo «<br>» ;
var_export ( mb_strlen ( $utf8 , ‘utf8’ ) ); // 32
?>

The easiest way to determine the character count of a UTF8 string is to pass the text through utf8_decode() first:

<?php
$length = strlen ( utf8_decode ( $s ));
?>

utf8_decode() converts characters that are not in ISO-8859-1 to ‘?’, which, for the purpose of counting, is quite alright.

When checking for length to make sure a value will fit in a database field, be mindful of using the right function.

There are three possible situations:

1. Most likely case: the database column is UTF-8 with a length defined in unicode code points (e.g. mysql varchar(200) for a utf-8 database).

<?php
// ok if php.ini default_charset set to UTF-8 (= default value)
mb_strlen ( $value );
iconv_strlen ( $value );
// always ok
mb_strlen ( $value , «UTF-8» );
iconv_strlen ( $value , «UTF-8» );

// BAD, do not use:
strlen ( utf8_decode ( $value )); // breaks for some multi-byte characters
grapheme_strlen ( $value ); // counts graphemes, not code points
?>

2. The database column has a length defined in bytes (e.g. oracle’s VARCHAR2(200 BYTE))

<?php
// ok, but assumes mbstring.func_overload is 0 in php.ini (= default value)
strlen ( $value );
// ok, forces count in bytes
mb_strlen ( $value , «8bit» )
?>

3. The database column is in another character set (UTF-16, ISO-8859-1, etc. ) with a length defined in characters / code points.

Find the character set used, and pass it explicitly to the length function.

PHP’s strlen function behaves differently than the C strlen function in terms of its handling of null bytes (‘\0’).

In PHP, a null byte in a string does NOT count as the end of the string, and any null bytes are included in the length of the string.

For example, in PHP:

strlen( «te\0st» ) = 5

In C, the same call would return 2.

Thus, PHP’s strlen function can be used to find the number of bytes in a binary string (for example, binary data returned by base64_decode).

We just ran into what we thought was a bug but turned out to be a documented difference in behavior between PHP 5.2 & 5.3. Take the following code example:

$attributes = array( ‘one’ , ‘two’ , ‘three’ );

if ( strlen ( $attributes ) == 0 && ! is_bool ( $attributes )) <
echo «We are in the ‘if’\n» ; // PHP 5.3
> else <
echo «We are in the ‘else’\n» ; // PHP 5.2
>

?>

This is because in 5.2 strlen will automatically cast anything passed to it as a string, and casting an array to a string yields the string «Array». In 5.3, this changed, as noted in the following point in the backward incompatible changes in 5.3 (http://www.php.net/manual/en/migration53.incompatible.php):

«The newer internal parameter parsing API has been applied across all the extensions bundled with PHP 5.3.x. This parameter parsing API causes functions to return NULL when passed incompatible parameters. There are some exceptions to this rule, such as the get_class() function, which will continue to return FALSE on error.»

So, in PHP 5.3, strlen($attributes) returns NULL, while in PHP 5.2, strlen($attributes) returns the integer 5. This likely affects other functions, so if you are getting different behaviors or new bugs suddenly, check if you have upgraded to 5.3 (which we did recently), and then check for some warnings in your logs like this:

strlen() expects parameter 1 to be string, array given in /var/www/sis/lib/functions/advanced_search_lib.php on line 1028

If so, then you are likely experiencing this changed behavior.

I would like to demonstrate that you need more than just this function in order to truly test for an empty string. The reason being that <?php strlen ( null ); ?> will return 0. So how do you know if the value was null, or truly an empty string?

<?php
$foo = null ;
$len = strlen ( null );
$bar = » ;

echo «Length: » . strlen ( $foo ) . «<br>» ;
echo «Length: $len <br>» ;
echo «Length: » . strlen ( null ) . «<br>» ;

if ( strlen ( $foo ) === 0 ) echo ‘Null length is Zero <br>’ ;
if ( $len === 0 ) echo ‘Null length is still Zero <br>’ ;

if ( strlen ( $foo ) == 0 && ! is_null ( $foo )) echo ‘!is_null(): $foo is truly an empty string <br>’ ;
else echo ‘!is_null(): $foo is probably null <br>’ ;

if ( strlen ( $foo ) == 0 && isset( $foo )) echo ‘isset(): $foo is truly an empty string <br>’ ;
else echo ‘isset(): $foo is probably null <br>’ ;

if ( strlen ( $bar ) == 0 && ! is_null ( $bar )) echo ‘!is_null(): $bar is truly an empty string <br>’ ;
else echo ‘!is_null(): $foo is probably null <br>’ ;

if ( strlen ( $bar ) == 0 && isset( $bar )) echo ‘isset(): $bar is truly an empty string <br>’ ;
else echo ‘isset(): $foo is probably null <br>’ ;
?>

// Begin Output:
Length: 0
Length: 0
Length: 0

Null length is Zero
Null length is still Zero

!is_null(): $foo is probably null
isset(): $foo is probably null

!is_null(): $bar is truly an empty string
isset(): $bar is truly an empty string
// End Output

So it would seem you need either is_null() or isset() in addition to strlen() if you care whether or not the original value was null.

There’s a LOT of misinformation here, which I want to correct! Many people have warned against using strlen(), because it is «super slow». Well, that was probably true in old versions of PHP. But as of PHP7 that’s definitely no longer true. It’s now SUPER fast!

I created a 20,00,000 byte string (

20 megabytes), and iterated ONE HUNDRED MILLION TIMES in a loop. Every loop iteration did a new strlen() on that very, very long string.

The result: 100 million strlen() calls on a 20 megabyte string only took a total of 488 milliseconds. And the strlen() calls didn’t get slower/faster even if I made the string smaller or bigger. The strlen() was pretty much a constant-time, super-fast operation

So either PHP7 stores the length of every string as a field that it can simply always look up without having to count characters. Or it caches the result of strlen() until the string contents actually change. Either way, you should now never, EVER worry about strlen() performance again. As of PHP7, it is super fast!

Here is the complete benchmark code if you want to reproduce it on your machine:

$iterations = 100000000 ; // 100 million
$str = str_repeat ( ‘0’ , 20000000 );

// benchmark loop and variable assignment to calculate loop overhead
$start = microtime ( true );
for( $i = 0 ; $i < $iterations ; ++ $i ) <
$len = 0 ;
>
$end = microtime ( true );
$loop_elapsed = 1000 * ( $end — $start );

// benchmark strlen in a loop
$len = 0 ;
$start = microtime ( true );
for( $i = 0 ; $i < $iterations ; ++ $i ) <
$len = strlen ( $str );
>
$end = microtime ( true );
$strlen_elapsed = 1000 * ( $end — $start );

// subtract loop overhead from strlen() speed calculation
$strlen_elapsed -= $loop_elapsed ;

echo «\nstring length: < $len >\ntest took: < $strlen_elapsed >milliseconds\n» ;

strlen

Строка ( string ), для которой измеряется длина.

Возвращаемые значения

Длина строки string в случае успеха, и 0, если string пуста.

Список изменений

Версия Описание
5.3.0 Ранние версии этой функции рассматривали массивы как строку Array, возвращая таким образом 5 в качестве длины этой строки, и вызывали ошибку уровня E_NOTICE .

Примеры

Пример #1 Пример использования strlen()

<?php
$str = ‘abcdef’ ;
echo strlen ( $str ); // 6

$str = ‘ ab cd ‘ ;
echo strlen ( $str ); // 7
?>

Примечания

Замечание:

Функция strlen() возвратит количество байт, а не число символов в строке.

Замечание:

Функция strlen() возвращает NULL при использовании на массивах, а также выводит ошибку уровня E_WARNING .

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *