<?PHP//regular expression//1. First regular expression if("a" = = "a"){ Echo"equal"; }Else{ Echo"noequal"; } Echo"; //the first parameter represents a matching pattern, and the second argument matches the string Echo(Preg_match('/php/', "php")); Echo(Preg_match('/php/', ' php222 ')); Echo(Preg_match('/php/', ' P2H2P2 ')); $modle= "/php/"; $string= "php"; if(Preg_match($modle,$string)) { Echo"success"; }Else{ Echo"fail"; }; Echo"; //what is a match? : The concept of matching//matching with equality is different//2. quantifier +: matches any of at least one leading string (one or more)//preamble: preceded by a character, + the leading is h//h + means as long as to include 1 H $model= "/ph+p/"; $string= "phhhhhhhp"; Echo(Preg_match($model,$string));//1 $model= "/ph+p/"; $string= "phhhgggggggggp"; Echo(Preg_match($model,$string));//0 $model= "/ph+p/"; $string= "pp"; Echo(Preg_match($model,$string))." //0//3. quantifier *: match either includes 0 or more leading strings (0 or more)//* although it can be 0, the leading characters cannot be changed, but they do not match. $model= "/ph*p/"; $string= "php"; Echo(Preg_match($model,$string));//1 $model= "/ph*p/"; $string= "pp"; Echo(Preg_match($model,$string));//1 $model= "/ph*p/"; $string= "pbbbbp"; Echo(Preg_match($model,$string));//0 Echo"; //3. Quantifier?: matches any of 1 or 0 leading characters $model= "/ph?p/"; $string= "pp"; Echo(Preg_match($model,$string));//1 $model= "/ph?p/"; $string= "php"; Echo(Preg_match($model,$string));//1 $model= "/ph?p/"; $string= "phhhp"; Echo(Preg_match($model,$string))." //0//4. quantifier (.) matches any one of the characters//n points matches n any character $model= "/p.p/"; $string= "php"; Echo(Preg_match($model,$string));//1 $model= "/p". p/"; $string= "phrp"; Echo(Preg_match($model,$string));//1 $model= "/p". p/"; $string= "php"; Echo(Preg_match($model,$string));//0 $model= "/p". p/"; $string= "phhhp"; Echo(Preg_match($model,$string))." //0//5. (. with *)//(. *) denotes any leading character, 0 or more $model= "/p.*p/"; $string= "php"; Echo(Preg_match($model,$string));//1 $model= "/p.*p/"; $string= "phhhhhhp"; Echo(Preg_match($model,$string));//1 $model= "/p.*p/"; $string= "pp"; Echo(Preg_match($model,$string))." //1//6. quantifier {x}: indicates that a preamble must be 3 $model= "/ph{3}p/";//h{3} matches 3 x H $string= "phhhp"; Echo(Preg_match($model,$string));//1 $model= "/ph{3}p/"; $string= "php"; Echo(Preg_match($model,$string))." //0//7. quantifier {x, y}: matches an x-y leading string $model= "/ph{3,5}p/"; $string= "phhhp"; Echo(Preg_match($model,$string));//1 $model= "/ph{3,5}p/"; $string= "phhhhp"; Echo(Preg_match($model,$string));//1 $model= "/ph{3,5}p/"; $string= "phhhhhp"; Echo(Preg_match($model,$string))." //1//8. quantifier {x,}: matches at least x leading string $model= "/ph{3,}p/"; $string= "phhhp"; Echo(Preg_match($model,$string));//1 $model= "/ph{3,}p/"; $string= "phhp"; Echo(Preg_match($model,$string));//0 $model= "/ph{3,}p/"; $string= "phhhhp"; Echo(Preg_match($model,$string))." //1//9.$: matches the line end of a string//$ generally loads the string on the tail, indicating a match starting from the tail (ending with Xxx) $model= "/php/"; $string= "CCCCCCCCCCCCCCCC php ccccccccccc"; Echo(Preg_match($model,$string));//1 $model= "/php$/"; $string= "CCCCCCCCCCCCCCCC php ccccccccccc"; Echo(Preg_match($model,$string));//0 $model= "/php$/"; $string= "cccccccccccccccccccccccccccphp"; Echo(Preg_match($model,$string))." //1//10. ^ to start from scratch match//expression starting with xxx $model= "/^php/"; $string= "cccccccccccccccccccccccccccphp"; Echo(Preg_match($model,$string));//0 $model= "/^php/"; $string= "phpcccccccccccccccccccccccccccphp"; Echo(Preg_match($model,$string));//1//if ^ and $ are used together to indicate that the match is identical, then use = = $model= "/^php$/"; $string= "phpcccccccccccccccccccccccccccphp"; Echo(Preg_match($model,$string))." //1//10.: Match String to the left or right//conditional selection symbol $model= "/php|jsp/"; $string= "php"; Echo(Preg_match($model,$string));//1 $model= "/php|jsp/"; $string= "jsp"; Echo(Preg_match($model,$string));//0 $model= "/php|jsp/"; $string= "asp"; Echo(Preg_match($model,$string));//0 $model= "/php|jsp|asp/"; $string= "asp"; Echo(Preg_match($model,$string))." //1//11. (): surround a character group or/ /talk about the back to understand?>
PHP Basic 30: Regular Match-quantifier