Home >Backend Development >PHP Tutorial >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.
$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!".
$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".
$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!".
$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:
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!