In this tutorial you will learn how regular expressions work, as well as how to lớn use them lớn perform pattern matching in an efficient way in PHP.
Bạn đang xem: Getting started with php regular expressions, php preg_match()
Regular Expressions, commonly known as "regex" or "RegExp", are a specially formatted text strings used khổng lồ find patterns in text. Regular expressions are one of the most powerful tools available today for effective & efficient text processing and manipulations. For example, it can be used to lớn verify whether the format of data i.e. Name, email, phone number, etc. Entered by the user was correct or not, find or replace matching string within text content, và so on.
PHP (version 5.3 and above) supports Perl style regular expressions via its preg_ family of functions. Why Perl style regular expressions? Because Perl (Practical Extraction và Report Language) was the first mainstream programming language that provided integrated tư vấn for regular expressions và it is well known for its strong support of regular expressions và its extraordinary text processing và manipulation capabilities.
Let"s begin with a brief overview of the commonly used PHP"s built-in pattern-matching functions before delving deep into the world of regular expressions.
preg_match() | 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 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. |
Note: The PHP preg_match() function stops searching after it finds the first match, whereas the preg_match_all() function continues searching until the end of the string and find all possible matches instead of stopping at the first match.
Regular expression syntax includes the use of special characters (do not confuse with the HTML special characters). The characters that are given special meaning within a regular expression, are: . * ? + < > ( ) ^ $ | . You will need lớn backslash these characters whenever you want to lớn use them literally. For example, if you want khổng lồ match ".", you"d have khổng lồ write .. All other characters automatically assume their literal meanings.
The following sections describe the various options available for formulating patterns:
Square brackets surrounding a pattern of characters are called a character class e.g.
Negated character classes can also be defined that match any character except those contained within the brackets. A negated character class is defined by placing a caret (^) symbol immediately after the opening bracket, like this <^abc>.
You can also define a range of characters by using the hyphen (-) character inside a character class, like <0-9>. Let"s look at some examples of character classes:
Matches any one of the characters a, b, or c. | |
<^abc> | Matches any one character other than a, b, or c. |
Matches any one character from lowercase a lớn lowercase z. | |
Matches any one character from uppercase a to uppercase z. | |
Matches any one character from lowercase a to uppercase Z. | |
<0-9> | Matches a single digit between 0 & 9. |
Matches a single character between a and z or between 0 & 9. |
The following example will show you how lớn find whether a pattern exists in a string or not using the regular expression and PHP preg_match() function:
Some character classes such as digits, letters, và whitespaces are used so frequently that there are shortcut names for them. The following table lists those predefined character classes:
. | Matches any single character except newline
. Xem thêm: phần mềm agribank cho iphone |
d | matches any digit character. Same as <0-9> |
D | Matches any non-digit character. Same as <^0-9> |
s | Matches any whitespace character (space, tab, newline or carriage return character). Same as < > |
S | Matches any non-whitespace character. Same as <^ > |
w | Matches any word character (definned as a to z, A to Z,0 to 9, & the underscore). Same as |
W | Matches any non-word character. Same as <^a-zA-Z_0-9> |
The following example will show you how khổng lồ find và replace space with a hyphen character in a string using regular expression & PHP preg_replace() function:
The following table lists the various ways to lớn quantify a particular pattern:
p+ | Matches one or more occurrences of the letter p. |
p* | Matches zero or more occurrences of the letter p. |
p? | Matches zero or one occurrences of the letter p. |
p2 | Matches exactly two occurrences of the letter p. |
p2,3 | Matches at least two occurrences of the letter p, but not more than three occurrences of the letter p. |
Matches two or more occurrences of the letter p. | |
p,3 | Matches at most three occurrences of the letter p |
The regular expression in the following example will splits the string at comma, sequence of commas, whitespace, or combination thereof using the PHP preg_split() function:
^p | Matches the letter p at the beginning of a line. |
p$ | Matches the letter p. At the over of a line. |
The regular expression in the following example will display only those names from the names array which start with the letter "J" using the PHP preg_grep() function:
i | Makes the match case-insensitive manner. |
m | Changes the behavior of ^ and $ to match against a newline boundary (i.e. Start or kết thúc 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 khổng lồ use whitespace and comments within a regular expression for clarity. |
The following example will show you how lớn perform a global case-insensitive search using the i modifier và the PHP preg_match_all() function.
Similarly, the regexp /car/ matches the words ending with the pattern car, và would match scar, oscar, or supercar, but would not match cart. Likewise, the /car/ matches the words beginning & ending with the pattern car, and would match only the word car.
The following example will highlight the words beginning with oto in bold:
$0";$text = "Words begining with car: cart, carrot, cartoon. Words ending with car: scar, oscar, supercar.";echo preg_replace($pattern, $replacement, $text);?>
We hope you have understood the basics of regular expression. Lớn learn how to validate size data using regular expression, please kiểm tra out the tutorial on PHP size Validation.
Is this trang web helpful to lớn you? Please give us a like, or giới thiệu your feedback khổng lồ help us improve. Connect with us on Facebook and Twitter for the latest updates.