Regular expressions in PHP
Regular Expressions, also referred to as regex or RegExp, are a specially formatted text strings used to find patterns in text. Regular expressions are one of the most powerful tools available today for efficient text processing and manipulations. It can be used to verify whether the format of data such as name, email, phone number, etc. entered by the user are correct. Regular expressions can also find or replace matching string within text content.
PHP version 5.3 and above supports Perl style regular expressions with the preg_ library of functions. Why Perl style regular expressions was the first mainstream programming language that provided integrated support for regular expressions and it is well known for its strong support of regular expressions and its extraordinary text processing and manipulation capabilities.
PHP regular expression functions
preg_match() Does a regular expression match. preg_match_all() Does a global regular expression match. preg_replace() Does a regular expression search and replace. preg_grep() Returns the elements of the input array that matched the pattern. preg_split() Splits up a string into substrings using a regular expression. preg_quote() Quote regular expression characters found within a string.
Character Classes
Character class is a pattern of character surrounded by square brackets such as [abc]. A character class always matches a single character out of a list of specified characters. What that means is that the the expression [abc] matches only a, b or c character. Negated character classes can also be defined, which is when a pattern matches any character except those contained within the brackets. A negated character class is defined by placing a caret character immediately after the opening bracket, like this [^abc]. We can also define a range of characters by using the hyphen character inside a character class, like [0-9]. There are also predefined character classes. Some character classes such as digits, letters, and whitespaces are so commonly used that there are shortcut names for them. It simplifies reading the regular expression and leads to less confusion.
Predefined Character classes
. Matches any single character except newline . d Equivalent to [0-9] D Equivalent to [^0-9] s Equivalent to [ ] ( tab, new line, carriage return ) S Equivalent to [^ ] w Equivalent to [a-zA-Z_0-9] W Equivalent to [^a-zA-Z_0-9]
PHP example using regular expressions
< ?php $pattern = "/Jo[be]/"; $text = "Joe just got a new Job."; $matches = preg_match_all($pattern, $text, $array); echo $matches . " matches were found."; ?>
Will output Joe and Job
PHP example using predefined character classes
PHP example using regular expressions
< ?php $pattern = "/s/"; $replacement = "-"; $text = "Hello World"; echo preg_replace($pattern, $replacement, $text); echo str_replace(" ", "-", $text); ?>
Will output Hello_World