Home > Article > Backend Development > Use the PHP function 'substr' to get the substring of a string
Use the PHP function "substr" to obtain the substring of a string
In PHP programming, you often encounter situations where you need to obtain part of the content of a string. At this time, we can use PHP's built-in function "substr" to achieve this. This article explains how to use the "substr" function to get a substring of a string and provides some code examples.
1. Basic usage of substr function
The substr function is used to obtain a substring of a specified length from a string. Its basic syntax is as follows:
substr(string $string, int $start, int $length): string
Parameter description:
$string
: The string to be operated on. $start
: Starting position. The index of the string starts from 0. $length
: The length of the substring. If the length is not specified, it defaults to the end of the string. Return value: Return the obtained substring.
The following is a simple example that demonstrates how to use the substr function to get a substring of a string:
$string = "Hello, World!"; $substring = substr($string, 0, 5); // 从位置0开始,获取长度为5的子串 echo $substring; // 输出 "Hello"
In the above example, we defined a string variable$ string
, whose value is "Hello, World!". Then, we use substr($string, 0, 5)
to get a substring of length 5 starting at position 0 and store the result in the variable $substring
. Finally, we use echo
to output the value of variable $substring
, which is "Hello".
2. Common usage examples
$string = "Hello, World!"; $substring = substr($string, -6); // 获取最后6个字符 echo $substring; // 输出 "World!"
In the above example, we use negative numbers as the starting number The starting position parameter means counting from the end of the string. substr($string, -6)
means getting the last 6 characters, which is "World!".
$string = "Hello, World!"; $substring = substr($string, 7, 5); // 从位置7开始,获取长度为5的子串 echo $substring; // 输出 "World"
In the above example, we pass substr($string, 7, 5)
Get the substring between position 7 and 11, that is, "World".
$string = "Hello, World!"; $substring = strtoupper(substr($string, 7, 5)); // 获取子串并转换为大写 echo $substring; // 输出 "WORLD"
In the above example, we will get it through the strtoupper
function Convert the substring to uppercase. strtoupper
Function is used to convert a string to uppercase.
3. Notes
When using the substr function, you need to pay attention to the following points:
$start
Yes A negative number means counting from the end of the string. $start
exceeds the length of the string, the returned substring is empty. $length
is 0 or negative, the returned substring is empty. To sum up, we can use the PHP function "substr" to get the substring of a string. By specifying the starting position and length parameters, we can flexibly obtain the desired substring. You can use the substr function to implement some common string operations, such as intercepting a specified range, getting the last few characters, etc.
Hope the above content can be helpful to everyone!
The above is the detailed content of Use the PHP function 'substr' to get the substring of a string. For more information, please follow other related articles on the PHP Chinese website!