Home >Backend Development >PHP Tutorial >substr() function in PHP: how to intercept part of a string

substr() function in PHP: how to intercept part of a string

WBOY
WBOYOriginal
2023-11-03 10:43:49909browse

substr() function in PHP: how to intercept part of a string

The substr() function in PHP: How to intercept part of a string requires specific code examples

In PHP programming, string processing is very common One of the operations. Intercepting part of a string is a requirement that is often encountered when processing strings. In PHP, we can use the built-in substr() function to intercept part of a string. This article will introduce the usage of substr() function in detail and give specific code examples.

The basic usage of the substr() function is as follows:
string substr(string $string, int $start[, int $length])

This function accepts three parameters: $string Represents the original string, $start indicates the starting position of interception, and $length indicates the length of interception. If the $length parameter is omitted, the portion starting from the $start position to the end of the string will be truncated.

Next we give several specific code examples to better understand the use of the substr() function.

  1. Intercept part of the string:
$str = "Hello, PHP!";
$substring = substr($str, 7);
echo $substring; // 输出:PHP!

In the above example, we intercept the part starting from position 7 of the string $str and assign the result to a variable $substring. Finally, the results are output to the screen through echo. The result will be "PHP!".

  1. Intercept a string of specified length:
$str = "Hello, PHP!";
$substring = substr($str, 7, 3);
echo $substring; // 输出:PHP

In the above example, we intercepted 3 characters of the string $str starting from position 7, and The result is assigned to the variable $substring. Output the value of $substring through echo, and the final result will be "PHP".

  1. Intercept the last few characters of the string:
$str = "Hello, PHP!";
$substring = substr($str, -4);
echo $substring; // 输出:PHP!

In the above example, we intercept the last few characters of the string by setting the $start parameter to a negative number. characters. Specifically, we set $start to -4, which means intercepting from the 4th character at the end of the string until the end of the string. So the final result will be "PHP!".

  1. Use the substr() function to intercept Chinese characters:
$str = "你好,PHP!";
$substring = mb_substr($str, 3, 3, 'UTF-8');
echo $substring; // 输出:PHP

Since Chinese characters are composed of multiple bytes, they cannot simply be intercepted using the substr() function. In this case, we need to use the mb_substr() function to intercept and specify the character encoding as UTF-8. In the above example, we intercepted 3 characters starting from position 3 of the string $str and assigned the result to the variable $substring. The final output is "PHP".

When using the substr() function, you need to pay attention to the following points:

  • $ The start parameter represents the starting position. If it is a negative number, it means starting from the end of the string. Calculate position.
  • The $length parameter represents the intercepted length. If it is omitted, it will be intercepted to the end of the string.
  • If you need to intercept a string containing Chinese characters, you should use the mb_substr() function and specify the character encoding.

This article introduces the usage of the substr() function in PHP through specific code examples. I hope it can help readers better understand and apply the substr() function to intercept part of a string.

The above is the detailed content of substr() function in PHP: how to 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