Extract string using substr() function in PHP

WBOY
Release: 2023-06-27 17:04:02
Original
1163 people have browsed it

Extracting strings using the substr() function in PHP

When developing web applications, string processing takes up a large part of the time and effort. PHP provides a wealth of string processing functions, one of which is very commonly used function is substr(). This function can be used to extract characters or strings of a specified length from a string.

The basic syntax of the substr() function is very simple: substr(string $string, int $start, int $length). Among them, $string is the string to be operated on, $start is the position to start extracting, and $length is the number of characters to be extracted.

For example, for a string $str = "Hello World!", if you want to extract 5 characters starting from the 6th character, you can use the substr() function:

$str = "Hello World!";
$sub = substr($str, 5, 5);
echo $sub; //输出 " World"
Copy after login

In this example, the first parameter of the substr() function is the string to be operated on, the second parameter is the position to start extracting (counting from 0), and the third parameter is the number of characters to be extracted. Therefore, the output result of the above code is "World".

If the third parameter (i.e. $length) is not specified, it will be extracted from the $start position to the end of the string. For example, if you want to extract all characters starting from the 6th character, you can write:

$str = "Hello World!";
$sub = substr($str, 5);
echo $sub; //输出 " World!"
Copy after login

In this example, the substr() function only specifies the first two parameters, so the 6th character is extracted. Characters starting with all characters, i.e. "World!"

In addition to regular string extraction, the substr() function can also perform some advanced operations. For example, you can specify a negative number for the $length parameter, which extracts characters starting from the end of the string. For example, if you want to extract all characters starting from the fifth character from the last, you can write:

$str = "Hello World!";
$sub = substr($str, -5);
echo $sub; //输出 "World!"
Copy after login

In this example, the second parameter of the substr() function is specified as -5, which means that you want to extract from Extraction starts from the fifth character from the last. Since the third parameter is not specified, all characters starting from the fifth character from the last are extracted, which is "World!".

In addition, the substr() function can also process multiple characters or strings at the same time. For example, if you want to extract all uppercase letters in a string, you can use a loop to iterate through all characters, determine whether each character is an uppercase letter, and then return a string composed of all uppercase letters:

$str = "Hello World!";
$sub = "";
for ($i = 0; $i < strlen($str); $i++) {
    $char = substr($str, $i, 1);
    if (ctype_upper($char)) {
        $sub .= $char;
    }
}
echo $sub; //输出 "HW"
Copy after login

In this example, the loop loops through each character in the string and uses the substr() function to obtain each character. Then, determine whether each character is an uppercase letter by calling the ctype_upper() function. Finally, if the character is an uppercase letter, it is added to the returned string. Finally, the output result is "HW".

In general, the substr() function is a very commonly used and very practical string processing function in PHP. Not only can it easily extract characters or strings of a specified length, it can also perform some advanced operations, such as extracting from the end, processing multiple characters or strings at the same time, etc. Proficient in the use of the substr() function allows us to process strings more efficiently in web development and improve development efficiency.

The above is the detailed content of Extract string using substr() function in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template