PHP Regular expression (regar expression) _php tips

Source: Internet
Author: User
Introduction: When you write a program or Web page that handles strings, you often find strings that match some complex rules

's needs. Regular expressions are the syntax used to describe these rules.
Example: In determining the user's email address format, mobile phone number format or collect other people's web content.
PHP is also used to regular expression PHP has two commonly used regular expression functions: Preg_match and Ereg.
I just saw preg_match today. Its specific wording is Preg_match (mode,string subject,array matches);
Here's a example I wrote.
Copy Code code as follows:

<?php
$mode = "/[^8s]/";//Match module
$str = "sssjj88d";//Match content
echo "if (Preg_match ($mode, $str, $arr)) {//Match function
echo "Match succeeded". $arr [0];//$arr [0]: matches the first value of the result set
}
else{
echo "Match failed";
}

Results:
Regular expression (regular expression) "meta character":
* matches the previous content 0 or more times, that is, the previous content any match
. Matching content 0 times 1 or more times, but does not include carriage return wrapping
+ 1 or more times (except empty) that match the previous content.
| Select match similar to PHP | (because this operation conforms to a weak type leading to the most complete match in the front)
^ Match String header content
$ Match String Tail content
{a,b}, which indicates the number of times the previous content was matched, from a to B.
() merges the whole match, and puts in the memory, can use \1 \2 ... Sequentially get
Here's a example I wrote in PHP:
Copy Code code as follows:

<?php
$mode = "/\d{2,4} (. *) \d{1,2}\\1\d{1,2}/";//the matching module is usually written as simple as possible.
$mode = "/2009 (. *) 9\\1 (10)/";
$str = "2011/9/10";
if (Preg_match ($mode, $str, $arr)) {
echo "Match succeeded". " <br/><font color=red> ". $arr [0]." </font><br/><br/><font size= ' +4 ' color=blue> teachers ' Day Happy </font>;
}
else{
echo "Match failed";
}
?>

Results:

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.