PHP Learning Path Six (PHP and regular expressions) _php tutorial

Source: Internet
Author: User
Tags ereg
[PHP]


<title>Regular expressions and style matching</title>


$find = "Love";//defines the variable to find
$result =preg_match ("/$find/", "I Love you,xiaohei!"); /The first parameter is the one you want to find
Variable, the second argument is a lookup string
Find "You" with a space before and after, return false
@ $result 1=preg_match ("\byou\b/", "I Love you,xiaohei!");
if ($result) {
echo "Match success!
";
}else {
echo "Match failed!
";
}
if ($result 1) {
echo "\byou\b/". " Match success!
";
}else {
echo "\byou\b/". " Match failed!
";
}

?>

/* ^ metacharacters are referred to as the beginning of the line end bit. Matches the style found at the beginning of the string.
* Judging whether there are Wangchangxu in the winning list ("Li Xingle Wangchangxu Black"), in judging, Wangchangxu, whether Li Xingle is the first
* */
$string = "Li Xingle Wangchangxu small Black";
$name 1 = "Wangchangxu";
$name 2 = "Li Xingle";
if (Ereg ("Wangchangxu", $string)) {//To determine if there are Wangchangxu
echo "has Wangchangxu!"
";
}
if (Ereg ("^ Li Xingle", $string)) {//Determine if the beginning of the line is Li Xingle
Echo ' Li Xingle is the first one
';
}
if (Ereg ("^ Wangchangxu", $string)) {
echo "Wangchangxu for the first
";
}
if (Ereg ("Small Black $", $string)) {//Determine if the end of the line is a small black
echo "Little Black is the last one
";
}

?>
Mailbox format judgment
Meta-character + is used to match the preceding subexpression one or more times: {2} Indicates the number of matches to be fed two times. {2,4} repeated 2-4 times
$myemail = "lixingle123@qq.com";
if (Ereg ([0-9a-za-z]+) ([@]) ([0-9a-za-z]+) ([.]) ([0-9a-za-z]{2,4}) ", $myemail)) {
echo $myemail. " Format is correct
";
}else {
echo $myemail. " The format is not correct, please re-enter!
";
}
?>
Match character formatting
Determine if the date format is YYYY-MM-DD format
$mydate = "2012-11-26";
if (Ereg ([0-9]{4}) (-) ([0-9]{2}) (-) ([0-9]{2}), $mydate, $regs)) {
echo $regs [1]. " Year $regs[3] Month $regs[5] Day
";
}else {
echo "Time format Error!
";
}




?>
/*php supports Perl-style regular expressions,
* Style matching function:
* int Preg_match (string $pattern, String $subject [, array $matches [, int flages]]);
* int Preg_match_all (string $pattern, String $subject [, array $matches [, int flages]]);
* Preg_match () and Preg_match_all () usage are similar, but the latter is the first one that matches the entire string, not the license, and returns the number of styles found
*
* */

$string = "I am studing php,php. PHP ";
$result =preg_match_all ("/php/i", $string, $matches, preg_offset_capture);

if ($result ==true) {
echo "$result PHP was found.
";
Print_r ($matches);
echo "
";
}else {
echo "PHP is not found.
";
Print_r ($matches);
echo "
";
}
?>





http://www.bkjia.com/PHPjc/477892.html www.bkjia.com true http://www.bkjia.com/PHPjc/477892.html techarticle [PHP] HTML head title regular expression and style match/title/head body? PHP $find =love;//defines the variable to find $result =preg_match (/$find/,i Love you, xiaohei!); /The first parameter is to ...

  • 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.