Функции для работы со строками

Discussion

Use cases

Large numeric literals are commonly used for business logic constants, unit test values, and performing data conversions. For example:

Composer’s retry delay when removing a file:

usleep(350000); // without separator
 
usleep(350_000); // with separator

Conversion of an Active Directory timestamp (the number of 100-nanosecond intervals since January 1, 1601) to a Unix timestamp:

$time = (int) ($adTime  10000000 - 11644473600); // without separator
 
$time = (int) ($adTime  10_000_000 - 11_644_473_600); // with separator

Working with scientific constants:

const ASTRONOMICAL_UNIT = 149597870700; // without separator
 
const ASTRONOMICAL_UNIT = 149_597_870_700; // with separator

Separating bytes in a binary or hex literal:

0b01010100011010000110010101101111; // without separator
 
0b01010100_01101000_01100101_01101111; // with separator
 
0x42726F776E; // without separator
 
0x42_72_6F_77_6E; // with separator

Use cases to avoid

It may be tempting to use integers for storing data such as phone, credit card, and social security numbers since these values appear numeric. However, this is almost always a bad idea, since such numbers often have prefixes and leading digits that are significant.

A good rule of thumb is that if it doesn’t make sense to use mathematical operators on a value (e.g. adding it, multiplying it, etc.), then an integer probably isn’t the best way to store it.

// don't do this:
$phoneNumber = 345_6789;
$creditCard = 231_6547_9081_2543;
$socialSecurity = 111_11_1111;

Will it be harder to search for numbers?

A concern that has been raised is whether numeric literal separators will make it more difficult to search for numbers, since the same value can be written in more than one way.

This is already possible, however. The same number can be written in binary, octal, decimal, hexadecimal, or exponential notation. In practice, this isn’t problematic as long as a codebase is consistent.

Furthermore, separators can sometimes make it easier to find numbers. To use an earlier example, 13_500 and 135_00 could be differentiated in a find/replace. Another example would be separated bytes in a hex literal, which allows searching for a value like “_6F_” to find only the numbers containing that specific byte.

Should it be the role of an IDE to group digits?

It has been suggested that numeric literal separators aren’t needed for better readability, since IDEs could be updated to automatically display large numbers in groups of three digits.

However, it isn’t always desirable to group numbers the same way. For example, a programmer may write differently depending on whether or not it represents a financial quantity stored as cents:

$total = 100_500_00; // represents $100,500.00 stored as cents
 
$total = 10_050_000; // represents $10,050,000

Binary and hex literals may also be grouped by a varying number of digits to reflect how they are used (e.g. bits may be separated into nibbles, bytes, or words). An IDE cannot do this automatically without knowing the programmer’s intent for each numeric literal.

Why resurrect this proposal?

Based on reading the discussion at the time, it didn’t receive enough positive votes because there weren’t many good use cases put forward for it. Also, the RFC had a short voting period of only 1 week.

Since that time, the ability to use underscores in numeric literals has been implemented in additional popular languages (e.g. Python, JavaScript, and TypeScript), and a stronger case can be made for the feature than was made before.

Comparison to other languages

Numeric literal separators are widely supported in other programming languages.

  • Ada: single, between digits

  • C#: multiple, between digits

  • C++: single, between digits (single quote used as separator)

  • Java: multiple, between digits

  • JavaScript and TypeScript: single, between digits

  • Julia: single, between digits

  • Kotlin: multiple, between digits

  • Perl: single, between digits

  • Python: single, between digits

  • Ruby: single, between digits

  • Rust: multiple, anywhere

  • Swift: multiple, between digits

Proposal

Enable improved code readability by supporting an underscore in numeric literals to visually separate groups of digits.

$threshold = 1_000_000_000;  // a billion!
$testValue = ‪107_925_284.88; // scale is hundreds of millions
$discount = 135_00;          // $135, stored as cents

Underscore separators can be used in all numeric literal notations supported by PHP:

6.674_083e-11; // float
299_792_458;   // decimal
0xCAFE_F00D;   // hexadecimal
0b0101_1111;   // binary
0137_041;      // octal

Restrictions

The only restriction is that each underscore in a numeric literal must be directly between two digits. This rule means that none of the following usages are valid numeric literals:

_100; // already a valid constant name
 
// these all produce "Parse error: syntax error":
100_;       // trailing
1__1;       // next to underscore
1_.0; 1._0; // next to decimal point
0x_123;     // next to x
0b_101;     // next to b
1_e2; 1e_2; // next to e

Integers

An integer is a number of the set ℤ = {…, -2, -1, 0, 1, 2, …}.

See also:

  • Arbitrary length integer / GMP
  • Floating point numbers
  • Arbitrary precision / BCMath

Syntax

Integers can be specified in decimal (base 10), hexadecimal (base 16), octal (base 8) or binary (base 2) notation. The negation operator can be used to denote a negative integer.

Binary integer literals are available since PHP 5.4.0.

To use octal notation, precede the number with a (zero). To use hexadecimal notation precede the number with 0x. To use binary notation precede the number with 0b.

As of PHP 7.4.0, integer literals may contain underscores (_) between digits, for better readability of literals. These underscores are removed by PHP’s scanner.

Beispiel #1 Integer literals


Formally, the structure for integer literals is as of PHP 7.4.0 (previously, underscores have not been allowed):

The size of an integer is platform-dependent, although a maximum value of about two billion is the usual value (that’s 32 bits signed). 64-bit platforms usually have a maximum value of about 9E18, except on Windows prior to PHP 7, where it was always 32 bit. PHP does not support unsigned integers. Integer size can be determined using the constant , maximum value using the constant since PHP 5.0.5, and minimum value using the constant since PHP 7.0.0.

Warnung

Prior to PHP 7, if an invalid digit was given in an octal integer (i.e. 8 or 9), the rest of the number was ignored. Since PHP 7, a parse error is emitted.

Integer overflow

If PHP encounters a number beyond the bounds of the integer type, it will be interpreted as a float instead. Also, an operation which results in a number beyond the bounds of the integer type will return a float instead.

Beispiel #2 Integer overflow on a 32-bit system

Beispiel #3 Integer overflow on a 64-bit system

There is no integer division operator in PHP. 1/2 yields the float 0.5. The value can be cast to an integer to round it towards zero, or the round() function provides finer control over rounding.

Converting to integer

To explicitly convert a value to integer, use either the (int) or (integer) casts. However, in most cases the cast is not needed, since a value will be automatically converted if an operator, function or control structure requires an integer argument. A value can also be converted to integer with the intval() function.

If a resource is converted to an integer, then the result will be the unique resource number assigned to the resource by PHP at runtime.

See also Type Juggling.

From floating point numbers

When converting from float to integer, the number will be rounded towards zero.

If the float is beyond the boundaries of integer (usually +/- 2.15e+9 = 2^31 on 32-bit platforms and +/- 9.22e+18 = 2^63 on 64-bit platforms other than Windows prior to PHP 7), the result is undefined, since the float doesn’t have enough precision to give an exact integer result. No warning, not even a notice will be issued when this happens!

Warnung

Never cast an unknown fraction to integer, as this can sometimes lead to unexpected results.

See also the .

From other types

Achtung

The behaviour of converting to integer is undefined for other types. Do not rely on any observed behaviour, as it can change without notice.

Integers

An integer is a number of the set ℤ = {…, -2, -1, 0, 1, 2, …}.

See also:

  • Arbitrary length integer / GMP
  • Floating point numbers
  • Arbitrary precision / BCMath

Syntax

Integers can be specified in decimal (base 10), hexadecimal (base 16), octal (base 8) or binary (base 2) notation. The negation operator can be used to denote a negative integer.

Binary integer literals are available since PHP 5.4.0.

To use octal notation, precede the number with a (zero). To use hexadecimal notation precede the number with 0x. To use binary notation precede the number with 0b.

As of PHP 7.4.0, integer literals may contain underscores (_) between digits, for better readability of literals. These underscores are removed by PHP’s scanner.

Example #1 Integer literals

Formally, the structure for integer literals is as of PHP 7.4.0 (previously, underscores have not been allowed):

The size of an integer is platform-dependent, although a maximum value of about two billion is the usual value (that’s 32 bits signed). 64-bit platforms usually have a maximum value of about 9E18, except on Windows prior to PHP 7, where it was always 32 bit. PHP does not support unsigned integers. Integer size can be determined using the constant , maximum value using the constant since PHP 5.0.5, and minimum value using the constant since PHP 7.0.0.

Warning

Prior to PHP 7, if an invalid digit was given in an octal integer (i.e. 8 or 9), the rest of the number was ignored. Since PHP 7, a parse error is emitted.

Integer overflow

If PHP encounters a number beyond the bounds of the integer type, it will be interpreted as a float instead. Also, an operation which results in a number beyond the bounds of the integer type will return a float instead.

Example #2 Integer overflow on a 32-bit system

Example #3 Integer overflow on a 64-bit system

There is no integer division operator in PHP. 1/2 yields the float 0.5. The value can be cast to an integer to round it towards zero, or the round() function provides finer control over rounding.

Converting to integer

To explicitly convert a value to integer, use either the (int) or (integer) casts. However, in most cases the cast is not needed, since a value will be automatically converted if an operator, function or control structure requires an integer argument. A value can also be converted to integer with the intval() function.

If a resource is converted to an integer, then the result will be the unique resource number assigned to the resource by PHP at runtime.

See also Type Juggling.

From floating point numbers

When converting from float to integer, the number will be rounded towards zero.

If the float is beyond the boundaries of integer (usually +/- 2.15e+9 = 2^31 on 32-bit platforms and +/- 9.22e+18 = 2^63 on 64-bit platforms other than Windows prior to PHP 7), the result is undefined, since the float doesn’t have enough precision to give an exact integer result. No warning, not even a notice will be issued when this happens!

Warning

Never cast an unknown fraction to integer, as this can sometimes lead to unexpected results.

See also the .

From other types

Caution

The behaviour of converting to integer is undefined for other types. Do not rely on any observed behaviour, as it can change without notice.

PHP Integers

An integer is a number without any decimal part.

2, 256, -256, 10358, -179567 are all integers. While 7.56, 10.0, 150.67 are floats.

So, an integer data type is a non-decimal number between -2147483648 and 2147483647. A value greater (or lower) than this, will be stored as float, because it exceeds the limit of an integer.

Another important thing to know is that even if 4 * 2.5 is 10, the result is stored as float, because one of the operands is a float (2.5).


Here are some rules for integers:

  • An integer must have at least one digit
  • An integer must not have a decimal point
  • An integer can be either positive or negative
  • Integers can be specified in three formats: decimal (10-based), hexadecimal (16-based — prefixed with 0x) or octal (8-based — prefixed with 0)

PHP has the following functions to check if the type of a variable is integer:

  • is_int()
  • is_integer() — alias of is_int()
  • is_long() — alias of is_int()

Example

Check if the type of a variable is integer:

<?php $x = 5985;var_dump(is_int($x));$x = 59.85;var_dump(is_int($x));?>

Время выполнения PHP скрипта

Замерить скорость выполнения всего PHP кода или какой-то отдельной его части, можно с помощью встроенной в PHP функции . Функция вернет UNIX штамп времени в микросекундах. Параметр true «говорит» функции вернуть число, а не строку, чтобы можно было потом просто отнять одно число от другого.

Логика проста: сначала замеряем текущее время в микросекундах, затем выполняем код, опять замеряем текущее время и высчитываем разницу во времени — эта разница и будет временем выполнения кода. С помощью функции форматируем полученную разницу в читаемый формат.

$start = microtime(true);

get_bloginfo('homeurl'); // какой-то код

$sec = sprintf('%.6F сек.', microtime(true) - $start );

echo "Время выполнения: $sec"; //> Время выполнения: 0.000014 сек.

9 ответов

Ответ обновлен на основе https://stackoverflow.com/a/9336130/315550 , спасибо — https://stackoverflow.com/users/116286/jb


ответ дан Mohammad Naji 11 September 2015 в 03:36

Мой единственный вопрос — должен ли был быть один регулярный оператор? Я бы пошел с двумя или тремя (потому что вам нужно создать небольшое сложное регулярное выражение, чтобы сделать это сразу.

Скажем, вам нужно иметь:

  • хотя бы один символ верхнего регистра
  • хотя бы один символ нижнего регистра
  • хотя бы один номер
  • имеет пароль длиной не менее 7 символов

Самое простое и эффективное решение:

В противном случае в одном регулярном выражении вам нужно будет рассмотреть несколько вариантов:

Объедините его в одно большое и трудно читаемое регулярное выражение, например:

Конечно, вы можете пойти (когда нужно просто проверить верхний и нижний регистр):

[1128 И все же придется проверять длину вручную. Второе решение кажется мне довольно неэффективным и трудным для чтения. Моя рекомендация: придерживайтесь первого.

ответ дан Vyktor 11 September 2015 в 03:36

Это работает чисто:

Чтобы увидеть его в действии, скопируйте его и вставьте сюда: http://phptester.net/index.php?lang=en

ответ дан Jonathan M 11 September 2015 в 03:36

Кажется, самый простой способ — это сделать это двумя регулярными выражениями.

Я полагаю, еще один способ сделать это ниже. Там написано «письмо, а потом в какой-то момент число (или наоборот)». Но вышеприведенный текст легче читать по ИМО.

ответ дан jb. 11 September 2015 в 03:36


PHP имеет встроенную функцию для этого. Это называется .

ответ дан Krii 11 September 2015 в 03:36

Следующие функции используют это преимущество и позволяют проверять буквы и цифры в нескольких алфавитах:

проверять наличие любой буквы в строке:

проверять строку, содержащую любое число

Использование

Оба вместе в соответствии с просьбой

ответ дан David D 11 September 2015 в 03:36

для проверки, содержит ли строка числа !!

ответ дан Nikita P 11 September 2015 в 03:36

Если вы хотите сопоставить только буквенно-цифровые символы (то есть считать строку недействительной, как только в ней есть что-то еще, например пробелы или специальные символы), это должно сработать.

В противном случае, просто удалите первый .

ответ дан Frosty Z 11 September 2015 в 03:36

Привет, вот несколько рекомендаций ниже:

Проблема: нет проверки на цифру внутри строки. Например,

Решение:

В нем говорится сначала символы, а затем цифры в некоторых точках.

ответ дан Gytis Tenovimas 11 September 2015 в 03:36

Другие вопросы по тегам:


С этим читают