Introduction to Preg in PHP

of 05

Preg_Grep PHP Function

The PHP function, preg_grep, is used to search an array for specific patterns and then return a new array based on that filtering. There are two ways to return the results. You can return them as is, or you can invert them (instead of only returning what matches, it would only return what does not match). It is phrased as: preg_grep ( search_pattern, $your_array, optional_inverse ).The search_pattern needs to be a regular expression. If you are unfamiliar with them this article gives you an overview of the syntax.

This code would result in the following data:
Array ( [4] => 4 [5] => 5 )
Array ( [3] => three [6] => six [9] => nine )

First, we assign our $data variable. This is a list of numbers, some in alpha form, others in numeric. The first thing we run is called $mod1. Here we are searching for anything that contains 4, 5, or 6. When our result is printed below we only get 4 and 5, because 6 was written as 'six' so it did not match our search.

Next, we run $mod2, which is searching for anything that contains a numeric character. But this time we include PREG_GREP_INVERT. This will invert our data, so instead of outputting numbers, it outputs all of our entries that were not numeric (three, six and nine).

of 05

Preg_Match PHP Function

The Preg_MatchPHP function is used to search a string and return a 1 or 0. If the search was successful a 1 will be returned, and if it was not found a 0 will be returned. Although other variables can be added, it is most simply phrased as: preg_match(search_pattern, your_string). The search_pattern needs to be a regular expression.

The code above uses preg_match to check for a key word (first juice then egg) and replies based on whether it is true (1) or false (0). Because it returns these two values, it is most often used in a conditional statement.​

of 05

Preg_Match_All PHP Function

Preg_Match_All is used to search a string for specific patterns and stores the results in an array. Unlike preg_match which stops searching after it finds a match, preg_match_all searches the entire string and records all matches. It is phrased as: preg_match_all (pattern, string, $array, optional_ordering, optional_offset).

In our first example, we use PREG_PATTERN_ORDER. We are searching for 2 things; one is the time, the other is it's am/pm tag. Our results are outputted to $match, as an array where $match[0] contains all matches, $match[1] contains all data matching our first sub-search (the time) and $match[2] contains all data matching our second sub-search (am/pm).

In our second example we use PREG_SET_ORDER. This puts each full result into an array. The first result is $match[0], with $match[0][0] being the full match, $match[0][1] being the first sub-match and $match[0][2] being the second sub-match.

of 05

Preg_Replace PHP Function

The preg_replace function is used to do a find-and-replace on a string or an array. We can give it one thing to find and replace (for example it seeks out the word 'him' and changes it to 'her'), or we can give it a full list of things (an array) to search for, each with a corresponding replacement. It is phrased as preg_replace ( search_for, replace_with, your_data , optional_limit, optional_count ) The limit will default to -1, which is no limit. Remember your_data can be a string or an array.

In our first example, we simply replace 'the' with 'a.' As you can see these are cAse seNsiTIvE. Then we set up an array, so in our second example, we are replacing both the words 'the' and 'cat.' In our third example, we set the limit to 1, so each word is only replaced one time. Finally, ​in our 4th example, we keep count of how many replacements we have made.

of 05

Preg_Split PHP Function

The function Preg_Spilit is used to take a string and put it into an array. The string is broken up into different values in the array based upon your input. It is phrased as preg_split ( split_pattern, your_data, optional_limit, optional_flags )

In the code above we perform three splits. In our first, we split the data by each character. In the second, we split it with a blank space, thus giving each word (and not each letter) an array entry. And in our third example, we use a '.' period to split the data, therefore giving each sentence its own array entry.

Because in our last example we use a '.' period to split, a new entry is started after our final period, so we add the flag PREG_SPLIT_NO_EMPTY so that no empty results are returned. Other available flags are PREG_SPLIT_DELIM_CAPTURE, which also captures the character you are splitting by (our "." for example) and PREG_SPLIT_OFFSET_CAPTURE, which captures the offset in characters where the split has occurred.

Remember that the split_pattern needs to be a regular expression and that a limit of -1 (or no limit) is the default if none is specified.

mla apa chicago
Your Citation
Bradley, Angela. "Introduction to Preg in PHP." ThoughtCo, Feb. 16, 2021, Bradley, Angela. (2021, February 16). Introduction to Preg in PHP. Retrieved from Bradley, Angela. "Introduction to Preg in PHP." ThoughtCo. (accessed May 30, 2023).