Regular expressions, as a quick and convenient tool for handling strings, are widely used in a variety of programming languages, and some of the techniques used in PHP are documented below.
My regular primer is an article originating from the Web [1], this article elaborated from the beginning to the use of the method, I think is a very good introductory material, but the study or to rely on individuals, in the process of use, or will continue to forget, so repeatedly read this article has four or five times, For some of the more difficult points of knowledge, even take a long time to digest, but as long as you can see the persistence of reading, you will find yourself to the regular use of the ability will be significantly improved.
Regular expression:
A syntax rule used to describe the character arrangement and matching pattern. It is mainly used for pattern segmentation, matching, lookup and substitution operations of strings.
Regular functions in PHP:
PHP has two sets of regular functions, the two functions are similar, respectively:
A set is provided by the Pcre (Perl compatible Regular Expression) library. A function named after a prefix using "preg_";
One set is provided by the POSIX (portable operating System Interface of Unix) extension. Use a function named "Ereg_" (the POSIX regular function library, which is not recommended after PHP 5.3, will be removed after PHP6)
Because POSIX is about to launch the history stage, and Pcre and Perl form the same, more conducive to our transition between Perl and PHP, so here focus on pcre regular use.
Pcre Regular Expression
Pcre is all called Perl compatible Regular Expression, meaning Perl-compatible regular expressions.
In Pcre, a pattern expression (that is, a regular expression) is typically included between two backslashes "/", such as "/apple/".
Some of the important concepts in the regular are: metacharacters, escapes, pattern units (repetition), antisense, references, and assertions, which can be easily understood and mastered in article [1].
Commonly used metacharacters (Meta-character):
Metacharacters description
\a an atom that matches the first string of strings
\z the atoms that match the tail of string strings
\b Matches the bounds of the word/\bis/the string that matches the header to the is/is\b/the string that matches the end is the/\bis\b/delimitation
\b matches any character except the word boundary/\bis/matches the "is" in the word "this"
\d match a number; equivalent to [0-9]
\d matches any character except a number; equivalent to [^0-9]
\w match an English letter, number, or underscore; equivalent to [0-9a-za-z_]
\w matches any character except English letters, numbers, and underscores; equivalent to [^0-9a-za-z_]
\s match a blank character; equivalent to [\f\t\v]
\s matches any one character except whitespace; equivalent to [^\f\t\v]
\f matches a page feed character equivalent to \x0c or \CL
Match a newline character; equivalent to \x0a or \CJ
Matching a return character is equivalent to \x0d or \cm
\ t matches a tab character, equivalent to \x09\ or \CL
\v matches a vertical tab; equivalent to \x0b or \ck
\onn match a octal number
\XNN matches a hexadecimal digit
\CC matches a control character
mode modifier (pattern modifiers):
The pattern modifier is especially used in ignoring case and matching multiple lines, and mastering this modifier often solves many of the problems we encounter.
I-can match uppercase and lowercase letters at the same time
M-Treat a string as multiple lines
S-Treats the string as a single line, and the newline character is treated as ordinary characters so that "." Match any character
X-whitespace negligible in pattern
U-Match to the nearest string
E-Use the replaced string as an expression
Format:/apple/i matches "Apple" or "apple" and so on, ignoring case. /I
Pcre Mode Unit:
1 extracting first-bit attributes
/^\d{2} ([\w]) \d{2}\\1\d{4}$ matches strings such as "12-31-2006", "09/27/1996", "86 01 4321". However, these regular expressions do not match the format of "12/34-5678". This is because the result "/" of the Mode "[\w]" has been stored. The match pattern is also the character "/" when the next position "\1" is referenced.
Use a non-storage mode unit when you do not need to store matching results (? :)”
For example/(?: ABC) (DEF) \\1g/will match "AEEg". In some regular expressions, it is necessary to use a non-storage-mode unit. Otherwise, the order of subsequent references needs to be changed. The previous example can also be written AS/(ABC) (CEF) \2g/.
Pcre Regular expression function:
Preg_match () and Preg_match_all ()
Preg_quote ()
Preg_split ()
Preg_grep ()
Preg_replace ()
The specific use of the function, we can find through the PHP manual, the following share some of the regular expressions accumulated:
Match Action Property
$str = ' <form name= ' ADFA "action=" asdf.bphp "target=" "><form name=" bbbb "action=" http://www.bac.com/test.php "Target=" Qwerqwerq "><form name=" bbbb "action=" http.php "target=" Qwerqwerq ">";
$match = ';
Preg_match_all ('/\s+action=\ ') (?!) http:) (. *?) \ "\s/', $str, $match);
Print_r ($match);
Using callback functions in regular
/**
* Replace some string by callback function
*
*/
function Callback_replace () {
$url = ' http://esfang.house.sina.com.cn ';
$str = ' <form name= ' ADFA "action=" asdf.bphp "target=" "><form name=" bbbb "action=" http://www.bac.com/test.php "Target=" Qwerqwerq "><form name=" bbbb "action=" http.php "target=" Qwerqwerq ">";
$str = Preg_replace ('/<=\saction=\ ') (?!) http:) (. *?) (? =\ "\s")/e ', ' Search (\ $url, \\1) ', $str);
Echo $str;
}
function Search ($url, $match) {
Return $url. '/' . $match;
}
A regular match with assertions
$match = ';
$str = ' <a href= ' ">xxxxxx.com.cn</a> _fcksavedurl=" ">xxxxxx.com.cn</a>" <b>bold font< /b> <p>paragraph text</p> ';
Preg_match_all ('/(?<=< (\w{1}) >). * (?=<\/\1>)/', $STR, $match);
echo "<br/> matches the contents of an HTML tag without attributes:";
Print_r ($match);
Replace the address in the HTML source code
$form _html = Preg_replace ('/(? <=\saction=\ "\ssrc=\" \shref=\ ") (?! Http:javascript) (. *?) (? =\ "\s")/e ', ' Add_url (\ $url, \ ' \\1\ ') ', $form _html);
Finally, the regular tools are powerful, but in terms of efficiency and writing time, there may be no explode to be more direct, and for some urgent or demanding tasks, simple, rough methods may be better.
And for the execution efficiencies between the Preg and Ereg two series, have seen the article said preg to be faster, specifically because of the use of ereg time is not much, but also to launch the historical stage, and then add all people prefer the way of pcre, so I do not do comparisons, familiar friends can make a comment, Thank you. The world of Little Wolves