Home  >  Article  >  Backend Development  >  PHP Regular Expression: How to extract a substring with a specific ending from a string

PHP Regular Expression: How to extract a substring with a specific ending from a string

王林
王林Original
2023-06-22 08:37:361111browse

In many PHP programs, we need to extract a substring with a specific ending from a string, such as extracting all file names ending with ".txt" from a set of file names. At this time, using regular expressions can help us implement this function quickly and effectively.

PHP provides many regular expression functions, the most commonly used of which are preg_match, preg_match_all and preg_replace. In this article, we will introduce how to use the preg_match and preg_match_all functions to extract substrings with specific endings from a string.

First, we need to specify the regular expression used to match a specific ending substring. In this example, we want to match all substrings ending with ".txt", so the matching regular expression should be "/.txt$/" . The regular expression consists of the following parts:

  • "/": the start tag of the regular expression.
  • ".": Matches the "." character. Since "." has a special meaning in regular expressions, it needs to be escaped with a backslash.
  • "txt": Matches the "txt" string.
  • "$": Matches the end of the string.
  • "/": The end tag of the regular expression.

Next, let’s see how to use the preg_match function to match the first substring ending with ".txt" from a string:

$string = "file1.txt file2.php file3.txt file4.html";
$pattern = "/.txt$/";
if (preg_match($pattern, $string, $match)) {
    echo "找到匹配的子字符串:".$match[0];
} else {
    echo "没有找到匹配的子字符串!";
}

In this example, We first define a string $string that contains multiple file names. Then, we define the regular expression $pattern to match the substring ending in ".txt" and call the preg_match function. The first parameter of the function is the regular expression, the second parameter is the string to be matched, and the third parameter is the matched result. If no result is found, the function returns false.

Finally, we determine whether a specific ending substring is matched by determining whether there are elements in the $match array. If there is, the substring is output. Otherwise, "No matching substring found!" is output.

Next, let’s see how to use the preg_match_all function to match all substrings ending with ".txt" from a string:

$string = "file1.txt file2.php file3.txt file4.html";
$pattern = "/.txt$/";
if (preg_match_all($pattern, $string, $matches)) {
    echo "找到 ".count($matches[0])." 个匹配的子字符串:<br>";
    foreach ($matches[0] as $match) {
        echo $match."<br>";
    }
} else {
    echo "没有找到匹配的子字符串!";
}

In this example, we also use $ The two variables string and $pattern define the strings and regular expressions that need to be matched. Then, we called the preg_match_all function. Unlike the preg_match function, this function can match all qualified substrings in a string, and the results are returned in an array.

When looping to output the matching results, we used the foreach loop to traverse the first element in the $matches array. Since this element is also an array, we use the $match variable to save the currently traversed substring and output it.

If no matching result is found, "No matching substring found!" will also be output.

To summarize, using regular expressions can help us extract specific ending substrings in strings quickly and efficiently. In practical applications, we can define different regular expressions according to actual needs and flexibly apply them in combination with functions such as preg_match, preg_match_all and preg_replace.

The above is the detailed content of PHP Regular Expression: How to extract a substring with a specific ending from 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