PHP learning path 6 (php and regular expressions) _ PHP Tutorial

Source: Internet
Author: User
PHP learning path 6 (php and regular expressions ). [Php] does htmlheadtitle match titleheadbody? Php $ findlove; defines the variable $ resultpreg_match ($ find, Iloveyou, xiaohei!) to be searched !); The first parameter is [php].


Regular expressions and style matching


$ Find = "love"; // defines the variable to be searched
$ Result = preg_match ("/$ find/", "I love you, xiaohei! "); // The first parameter is
// Variable. The second parameter is the search string.
// Search for "you" with leading and trailing spaces. false is returned.
@ $ Result1 = preg_match ("\ byou \ B/", "I love you, xiaohei! ");
If ($ result ){
Echo "matched!
";
} Else {
Echo "matching failed!
";
}
If ($ result1 ){
Echo "\ byou \ B/". "matching successful!
";
} Else {
Echo "\ byou \ B/". "matching failed!
";
}

?>

/* ^ Metacharacters are called the first end of a line .. Matches the style found from the start of the string.
* Judge whether there is Wang changchao in the award list ("Li Xingle Wang changchao and Xiao Hei"). if Wang changchao and Li Xingle are the first winners
**/
$ String = "Li Xingle Wang changchao xiao ";
$ Name1 = "Wang changchao ";
$ Name2 = "Li Xingle ";
If (ereg ("Wang changchao", $ string) {// determine whether there is Wang changchao
Echo "Wang changchao!
";
}
If (ereg ("^ Li Xingle", $ string) {// determines whether the first line is Li Xingle
Echo 'Li Xingle is the first
';
}
If (ereg ("^ Wang changchao", $ string )){
Echo "Wang changchao is the first
";
}
If (ereg (" $", $ string) {// determines whether the end of a row is a
Echo "Xiao Hei is the last
";
}

?>
// Determine the email format
// Metacharacters + are used to match the previous subexpression one or more times. {2} indicates that the matching times are fed twice. {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. "The format is correct.
";
} Else {
The echo $ myemail. "format is incorrect. please enter it again!
";
}
?>
// Match character format
// Determine whether the date format is in YYYY-MM-DD format
$ Mydate = "2012-11-26 ";
If (ereg ("([0-9] {4}) (-) ([0-9] {2 })(-) ([0-9] {2}) ", $ mydate, $ regs )){
Echo $ regs [1]. "$ regs [3] $ regs [5]
";
} Else {
Echo "time format error!
";
}




?>
/* PHP supports regular expressions in the Perl style,
* Style matching functions:
* 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 () are similar in usage, but the latter matches the entire string instead of the first one for the license and returns the number of styles found.
*
**/

$ String = "I am studing PHP, PHP is so easy. 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 was not found.
";
Print_r ($ matches );
Echo"
";
}
?>





Http://www.bkjia.com/PHPjc/477892.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/477892.htmlTechArticle[php] html head title regular expression and style match/title/head body? Php $ find = love; // defines the variable $ result = preg_match (/$ find/, I love you, xiaohei!) to be searched !); // The first parameter is...

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.