Methods used in PHP regular expressions

Source: Internet
Author: User
Tags ereg html tags modifier php regular expression posix preg regular expression

PHP regular expressions are used primarily for string pattern segmentation, matching, lookup, and substitution operations. Using regular expressions can be inefficient in some simple environments, so how to better use PHP regular expressions requires a comprehensive consideration.





my PHP is getting started, is the origin of an article on the Internet, this article elaborated the PHP regular expression using the method, I think is a very good introductory material, but the study still depends on the individual, in the use of the process, or will continue to forget, So repeatedly read this article has four or five times, for some of the more difficult knowledge points, even take a long time to digest, but as long as you can see adhere to read, you will find yourself for the regular use of the ability will be significantly improved.





PHP Regular expression definition:





A syntax rule used to describe character permutations and matching patterns. 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:




The
set is provided by the Pcre (Perl compatible Regular Expression) library. A function named with "Preg_" as a prefix;





is provided by the POSIX (portable operating System Interface of Unix) extensions. Use a function named "Ereg_" (the POSIX regular library, which is not recommended since PHP 5.3, will be removed after PHP6)





because POSIX is about to launch the history arena, and Pcre and Perl form the same, more conducive to our transition between Perl and PHP, so here is a 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
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):





Meta character Description





A matches the atomic
of the string header




Z-matching atomic
of string tail




b matches the bounds of the word/bis/the string/isb/matching the header to the is the string/bisb/delimitation





B matches any character except the word boundary/bis/matches the "is" in the word "this"





d matches a number; equivalent to [0-9]





D matches any character other than a number; equivalent to [^0-9]





W matches 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 matches a white-space character; equivalent to [FTV]





S matches any one character except the white space character; equivalent to [^FTV]





f matches a page feed character equivalent to x0c or CL





matches a newline character, equivalent to x0a or CJ





matches a return character 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 matches 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 both uppercase and lowercase





M-Treats 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-The whitespace in the pattern ignores





U-Match to the nearest string





e-Uses the substituted string as an expression





format:/apple/i matches "Apple" or "apple" and so on, ignoring case. /I





pcre mode unit:





//1 extracts the first-bit attributes





/^d{2} ([W]) d{2}1d{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]" is already stored. When the next position "1" is referenced, its matching pattern is also the character "/".





use a non-storage mode unit when no matching results are needed (? :)”





For example/(?: A|b|c) (d| e| F) 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 above example can also be written/(A|B|C) (c| e| F) 2g/.

pcre Regular expression functions:

Preg_match () and Preg_match_all ()  
Preg_quote ()  
Preg_ The specific use of the split ()  
Preg_grep ()  
Preg_replace ()  

function can be found by using the PHP manual, which shares some regular expressions that are accumulated:

matches the Action property

$str  =  '; 
$match  =  '; 
Preg_ Match_all ('/s+action= ') (?!) http:) (. *?) " s/',  $str,  $match); 
Print_r ($match);  


Use callback functions in regular

/** 
* replace some string by callback function 

*/ 
Function callback_replace ()  { 
$url  =  ' http://esfang.house.sina.com.cn '; 
$str  =  '; 
$str  = preg_replace  (  '/(? <=saction=) ') (?! http:) (. *?) (? = "s)/e ',  ' Search ($url,  1) ',  $str  ); 

echo  $str; 


Function search ($url, &NBSP; $match) { 
return  $url  .  '/'  .  $match; 


A regular match with assertions

$match  =  '; 
$str  =  ' Xxxxxx.com.cn bold font  
paragraph text 

'; 
preg_match_all  (  '/(?<=< (W{1}) >). * (? =</1>)/',  $str,  $match  ); 
echo  matches content in HTML tags with no attributes: "; 
print_r  (  $match  );  

replace address in HTML source code

$form _html = preg_ replace  (  "<=saction=" |ssrc= "|shref=") (?! Http:|javascript) (. *?) (? = "s)/e ',  ' add_url ($url,  ' 1 ') ',  $form _html  ');  

Finally, the regular tool, though powerful, But in terms of efficiency and writing time, there may be times when there is no explode to be more direct, and for some urgent or demanding tasks, a simple, crude approach may be better.

and for the execution efficiency between the Preg and Ereg two series, I 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 stage of history, and add everyone more preference to the pcre of the way

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.