PHP Regular Expressions
| Function | What it Does |
|---|---|
| preg_macth () | Perform a regular expression match. |
| preg_match_all () | Perform a global regular expression match. |
| preg_replace () | Perform a regular expression search and replace. |
| preg_grep () | Returns the elements of the inputs array that matched the pattern. |
| preg_split () | Splits a string into substring using a regular expression. |
| preg_quote () | Quote regular expression characters found within a string. |
| RegExp | What it Does |
|---|---|
| [abc] | Matches any one of the characters a,b, or c. |
| [^abc] | Matches any one character other than a, b, c. |
| [a-z] | Matches any one character from lowercase a to lowercase z. |
| [A-Z] | Matches any one character from uppercase a to uppercase z. |
| [a-Z] | Matches any one character from lowercase to uppercase Z. |
| [0-9] | Matches a single digit between 0 and 9. |
| [a-z0-9] | Matches a single character between a and z or between 0 to 9. |
|
1 2 3 4 5 6 7 8 |
<?php $pattern="/ca[cf]e/"; $text="He was eating cake in the cafe."; if(preg_match($pattern, $text)){ echo "Match found!"; } else { echo "Match not found"; } |
|
1 2 3 4 5 |
<?php $pattern="/ca[cf]e/"; $text="He was eating cake in the cafe."; $matches=preg_match_all($pattern, $text, $array); echo $matches. "matches were found". |
| Shortcut | Function |
| . | It matches a single character except for only a new line \n |
| \d | It matches any digit character. Similar to [0-9] |
| \D | It matches any non-digit character. Similar to [^0-9] |
| \s | It matches the whitespace character. Similar to [ \t\n\r] |
| \w | It will match any word character and underscore. Similar to [a-zA-Z_0-9] |
| \W | It will match any non-word character. Similar to [^a-zA-Z_0-9] |
|
1 2 3 4 5 6 7 8 9 |
<?php $pattern="/\s/"; $replacement="-"; $text="Earth revolves around\nthe\tSun"; // Replace spaces, newlines and tabs echo preg_replace ($pattern, $replacement, $text); echo "<br >"; // Replace only spaces echo str_replace (" ","-", $text); |
| RegExp | What it Does |
| p+ | Matches one or more occurrences of the letter p. |
| p* | Matches zero or more occurrences of the letter p. |
| p? | Matches zero or more occurrences of the letter p. |
| p{2} | Matches exactly two occurrences of the letter p. |
| p{2,3} | Matches at least two occurrences of the letter p, but not more than three occurrences of the letter p. |
| p{2,} | Matches two or more occurrences of the letter p. |
| p{,3} | Matches at most three occurrences of the letter p. |
|
1 2 3 4 5 6 7 8 9 |
<?php $pattern="/[\s,]+/"; $text="My favourite color are red, green and blue"; $parts=preg_split ($pattern, $text); // Loop through parts array and display substrings foreach($parts as part){ echo $part."<br>" } |
| RegExp | Function |
| ^p | It will match the letter p at the start of a line. |
| p$ | It will match the letter p at the end of a line. |
|
1 2 3 4 5 6 7 8 |
<?php $pattern="/^J/"; $names=array("John Carter","Clark Kent", "John Rambo"); $matches=preg_grep($pattern,$names); //Loop through matches array and display matched names foreach($matches as $match){ echo $match."<br>" } |
| Modifier | What is Does |
| i | Makes the match case-insensitive manner. |
| m | Changes the behavior of ^ and $ to match against a newline boundary (i.e. start or end of each line within a multiline string), instead of a string boundary. |
| g | Perform a global match i.e. finds all occurrences. |
| o | Evaluates the expression only once. |
| s | Changes the behavior of . (dot) to match all characters, including newlines. |
| x | Allows you to use whitespace and comments within a regular expression for clarity. |
| Modifier | What is Does |
| i | Makes the match case-insensitive manner. |
| m | Changes the behavior of ^ and $ to match against a newline boundary (i.e. start or end of each line within a multiline string), instead of a string boundary. |
| g | Perform a global match i.e. finds all occurrences. |
| o | Evaluates the expression only once. |
| s | Changes the behavior of . (dot) to match all characters, including newlines. |
| x | Allows you to use whitespace and comments within a regular expression for clarity. |
|
1 2 3 4 5 6 |
<?php $pattern='/\bcar\w*/'; $replacement='<b>$0</b>'; $text='Words beginning with car: cart, carrot, cartoon words ending with car: scar, oscar, supercar'; echo preg_replace ($pattern, $replacement, $text); |
