PHP preg_grep() Tutorial 🎯

beginner
17 min

PHP preg_grep() Tutorial 🎯

Welcome to our PHP preg_grep() tutorial! In this lesson, we'll explore how to use the preg_grep() function to search for and filter matching patterns within an array. Let's dive in! 🐳

What is preg_grep()? πŸ“

In PHP, preg_grep() is a built-in function that searches for a regular expression pattern within an array and returns an array containing all the elements that match the pattern. This function is particularly useful when you need to filter or extract specific data from an array.

How to use preg_grep() πŸ’‘

To use preg_grep(), you need to follow these steps:

  1. Create an array containing the data you want to search.
  2. Define the regular expression pattern you want to match.
  3. Call the preg_grep() function and pass the array and pattern as arguments.
  4. The function will return a new array containing only the elements that match the pattern.

Let's look at an example:

php
<?php $data = array("apple", "banana", "orange", "grape", "watermelon"); $pattern = "/a+le/"; // This pattern matches any word ending with "ale" $matches = preg_grep($pattern, $data); print_r($matches); ?>

In this example, we have an array of fruits, and we're searching for any words ending with "ale." The output will be:

Array ( [3] => Array ( [1] => banana [3] => grape ) )

As you can see, only the elements "banana" and "grape" were returned since they matched the pattern.

Regular Expressions πŸ’‘

If you're not familiar with regular expressions, don't worry! We'll provide a brief introduction to them in our upcoming tutorials. For now, let's focus on using preg_grep() with some basic patterns.

Pro Tip πŸ’‘

  • If you want to case-insensitive searches, simply add the 'i' modifier at the end of your pattern: "/pattern/i".
  • You can also use the 'm' modifier to treat the pattern as multiple lines: "/pattern/m".

Quiz 🎯

Quick Quiz
Question 1 of 1

What does the preg_grep() function do in PHP?

Now that you have a basic understanding of PHP's preg_grep() function, let's move on to a more advanced example:

php
<?php $emails = array( "john.doe@example.com", "jane_doe@example.com", "john@example.co.uk", "jane_smith@example.co.uk", "johndoe+info@example.com", "jane.doe+work@example.com" ); $pattern = "/\A[\w._%+-]+@[\w.-]+\.[a-zA-Z]{2,}\z/"; // This pattern matches a valid email address $validEmails = preg_grep($pattern, $emails); print_r($validEmails); ?>

In this example, we have an array of email addresses, and we're searching for valid email addresses. The output will be:

Array ( [0] => Array ( [0] => "john.doe@example.com" [1] => "jane_doe@example.com" [3] => "jane_smith@example.co.uk" [4] => "johndoe+info@example.com" [5] => "jane.doe+work@example.com" ) )

As you can see, all the email addresses in the array were returned since they matched the pattern.

Summary βœ…

In this tutorial, we learned about PHP's preg_grep() function, which allows us to search for a regular expression pattern within an array and filter the array based on the matches. We also covered some basic regular expressions and saw examples of both simple and more complex use cases.

In our next tutorial, we'll dive deeper into regular expressions and explore more advanced patterns. Until then, happy coding! πŸ‘‹