#### Example 2: Simple Pattern Matching

In this example, we’ve used pattern matching. As it’s pattern matching, the $matches[0] element contains the text which matched the full pattern, and the $matches[1] element contains the text which matched the first parenthesized pattern.

#### Example 3: Find Image Paths

Next, we’re trying to find the value of the src attribute in the first <img> tag. It’s important to note that it returns only the first match, even though there are multiple matches.

#### Example 4: Find Specific HTML Tags

Finally, here's an example which demonstrates how to extract text from within the specific HTML tags.

### The preg_match_all Function

The preg_match_all function is very similar to the preg_match function, with the exception that it performs a global regular expression match. And thus, it returns all the values that are matched against the pattern.

Also, the syntax of the preg_match_all function is the same as that of the preg_match function.

Let’s quickly go through a couple of examples to see how it works. In fact, we’ll revise the examples that we’ve discussed in the previous section with the preg_match_all function instead of the preg_match function. In this way, it should help you to understand the difference between these two variants.

#### Example 4: Find Specific HTML Tags

It’s important to notice that when you use the preg_match_all function, the $matches variable is initialized with a multidimensional array. The $matches[0] element is initialized with an array of all the values that are matched against the full pattern. On the other hand, the $matches[1] element is initialized with an array of values that are matched against the first parenthesized sub-pattern, the $matches[2] element contains an array of values that are matched against the second parenthesized sub-pattern, and so on.

The preg_match_all function is a very powerful function, and you will come across it often in your day-to-day PHP development.

### The preg_replace Function

The preg_replace function performs a regular expression search and allows you to replace the search results with other strings. Let's look at its format:

### The preg_split Function

The preg_split function allows you to split a string by a regular expression.

Let’s have a look at the syntax of the preg_split function.

The first argument is the pattern which you want to search for, and the second argument is the input string.

Let’s have a look at the following example to understand how it works.

Here you can see we use a pattern that matches line breaks, and the output returns the individual lines of the text.

## Conclusion

Today, we discussed regular expression functions in PHP. We explored a variety of preg_* family functions, along with real-world examples to understand how they work.