Home  >  Article  >  Backend Development  >  PHP function introduction—substr(): intercept part of a string

PHP function introduction—substr(): intercept part of a string

王林
王林Original
2023-07-24 21:33:155482browse

PHP function introduction—substr(): intercept part of a string

In PHP, string processing is a very common operation. For a string, sometimes we need to intercept part of the content for processing. At this time, PHP provides a very practical function-substr().

The substr() function can intercept part of a string. The specific usage is as follows:

string substr ( string $string , int $start [, int $length ] )

Function parameter description:

  • $string: to be intercepted string.
  • $start: The position to start interception, the position is calculated from zero.
  • $length: optional parameter, indicating the length to be intercepted. If this parameter is not set, it will be intercepted from the $start position to the end of the string.

Below, let’s look at a few specific examples to better understand the usage of the substr() function.

Example 1: Intercept part of the string

$str = "Hello, World!";
$sub_str = substr($str, 0, 5);
echo $sub_str; // 输出 "Hello"

In the above example, we intercept the first 5 characters of the string "$str" and assign them to the variable "$sub_str". Finally, the contents of the variable "$sub_str" are output. The output is "Hello".

Example 2: Intercept part of the string (no length specified)

$str = "Hello, World!";
$sub_str = substr($str, 7);
echo $sub_str; // 输出 "World!"

In the above example, we intercept the string "$str" starting from the 7th character and assign it to The variable "$sub_str", since the length is not specified, will intercept the content from the 7th character to the end of the string. Finally, the contents of the variable "$sub_str" are output. The output is "World!".

Example 3: Intercept part of the string (negative number as parameter)

$str = "Hello, World!";
$sub_str = substr($str, -6, 5);
echo $sub_str; // 输出 "World"

In the above example, we intercept the string "$str" starting from the 6th character from the bottom and assign it a value Give the variable "$sub_str" and specify the length to be intercepted as 5. Finally, the contents of the variable "$sub_str" are output. The output is "World".

Through the above example, we can see that the substr() function is very flexible and can intercept part of the string according to needs.

It should be noted that if the parameter $start exceeds the length of the string, the returned substring will be empty. And if the parameter $length is negative, it will be treated as 0. Of course, we can also use negative numbers as parameters to intercept from the end of the string.

Summary:
The substr() function is a very commonly used function in string processing, through which we can easily intercept part of the string. We can flexibly use the $start and $length parameters to control the position and length of the interception. Mastering the use of the substr() function, we can process strings more easily. I hope that through the introduction of this article, readers will have a deeper understanding of the substr() function.

The above is the detailed content of PHP function introduction—substr(): intercept part of a string. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn