
Regular expression is so useful that most of the popular programming language includes an engine to support this amazing pattern-matching concept. Today, I will try to describe the basic regular expression implementation concept in the PHP programming language. If you are comparatively very new at regular expression programming, you might Want to read about regular expression basics first, which I have discussed earlier on this blog, intended for complete beginners.
Basic PHP Regular Expression Functions:
PHP has several core handy functions that utilize regular expression. All of them are with ‘preg_’ prefix in the method name. If you are using an ID that supports intelligence on PHP functionality(Like Netbeans, PHPStorms, etc.), you can see the list of those functions by typing this prefix. Here are some of the most commonly used functions, along with some code samples on how to use those functions:
preg_grep : This method matches a set of strings against a given pattern. This takes 2 parameters , one is the pattern against which to be matched and an array list of strings, which are to be matched. This method returns an array which consists a filtered list of the matched array strings those matched successfully against the pattern.
$matches = preg_grep("/{pattern}/i",$input_array);
print_r($matches);
Code language: PHP (php)
preg_match: This method finds the occurrence of one or more pattern-matched sub-strings inside another string. This method also takes two parameters: one is the pattern against which to be matched and a string value which is to be matched. It returns either 0 or 1, depending on whether any match is found or not. It’s good for getting a generic true/false result whether a match is found or not.
if (preg_match("/test/i", "A very simple test message for php regex tutorial example.")) {
echo "Input string matched with the pattern";
} else {
echo "Input string didn't match with the pattern";
}
Code language: HTML, XML (xml)
This function also takes an optional third array parameter as a reference; all matched strings/substring is returned on that referenced variable.
$matches = array();
preg_match("/test/i", "simple test with two test substring", $matches);
echo "matched strings are: \n";
print_r($matches);
Code language: HTML, XML (xml)
preg_match_all : This is same as the previous method. Only difference is, it returns exact number of times a match found.Also it takes an extra parameter and reference variable where all matches are stored(this parameter was optional in case of preg_match).
$input = {input string to be matched};
echo "matches found ".preg_match_all("/{pattern}/", $input, $matches)." times";
//use can use $matches as an array list of matched string
Code language: HTML, XML (xml)
preg_quote : This method is used to escape regular expression-specific special characters(the characters that are used as delimiters in regex pattern). It takes the input string that is to be escaped and returns the resultant string.
$input_string = 'price of a item is $30';
$input_string = preg_quote($keywords);
echo $input_string; //put an '\' character in front of the '$' sign as that is used as an regular expression syntax
Code language: PHP (php)
preg_replace : This method is used to replace a specific type of sub-strings that matches a pattern inside long string texts. This takes 3 parameters: one is the pattern, one is the replace terms to be used, and the last one is the long string that is to be searched for.
$string = 'April 15, 2003';
$pattern = '/(\w+) (\d+), (\d+)/i';
$replacement = 'Month: ${1} <br/> Date:${2} <br/> Year:${3}';
echo preg_replace($pattern, $replacement, $string);
Code language: PHP (php)
preg_split : This method is used to split up a long strings in some places where specific patterns are found. This takes 2 parameters, one is the pattern and other is the input string. I returns an array of strings containing the spitted strings.
$str = '{a long string to split}';
$chars = preg_split('/{pattern}/', $str);
print_r($chars);
Code language: PHP (php)
Further References:
Hopefully, this regular expression tutorial in PHP code examples will help you only to develop the basic concept. Besides, PHP has very good well documentation for its core functionality references. The same applies to regular expression functions. You can see the function list on php.net regular expression functions list page. There, you will also find individual links for each function. Each of them has basic documentation with function signatures, along with examples provided by the community. I hope they will help you better understand. If you are facing any issues, please let me know. Happy coding 🙂
Leave a Reply