A regular expression describes a string matching mode. This mode is used to match, search, replace, and split a string in a specific function. As a matching template, regular expressions are composed of atoms (common characters, such as characters A to Z) and special characters (such as *, +, and? And the pattern modifier. In the simplest regular expression mode, at least one atom must be included, such as "//". In addition, when using a pattern in a Perl-Compatible Regular Expression Function, you must add a separator to the pattern, that is, the pattern is included between two Backslash. The Regular Expression Pattern of an HTML connection is as follows:
'/<.*? (? : |\\ T | \\ R |\\ N )? Href = [\ '"]? (. + ?) [\ '"]? (? :(? : | \ T | \ r | \ n) +. *?)?> (. + ?) <\/A. *?> /Sim '// match the connected regular expression. Any valid HTML connection tag in the webpage can match the regular expression pattern. In this mode, the atomic, metacharacter, and pattern modifier of the regular expression template are used.★The delimiter uses two slash "/" to declare the pattern between them.★Atoms use common characters such as <, A, href, =, ', ",/, and> and escape characters such as \ t, \ r, and \ n.★Metacharacters use [], (), |,., and ,.,? , *, +, And other characters with special meanings.★The pattern modifiers used are the three characters "S", "I", and "M" after the last slash of the delimiter ". The usage of atoms, metacharacters, and pattern delimiters will be detailed later. First, write an example to understand the application of regular expressions. Use the preg_match () function provided by PHP to convert the regular expression defined in the previous example. This function has two required parameters. The first parameter needs to provide the regular expression mode compiled by the user, and the second parameter needs a string. This function is used to search for the first parameter in the second string parameter to match the correct expression. Returns true if the match is successful. The Code is as follows:
| 1234567891011 |
<?php $pattern=‘/<a.*?(?:|\\t|\\r|\\n)?href=[\‘"]?(.+?)[\‘"]?(?:(?:|\\t|\\r|\\n)+.*?)?>(.+?)<\/a.*?>/sim‘; $content="Click to go to <a href = 'HTTP: // php.ncong.com '> encong php </a> learning blog. "; // Use the preg_match () function to perform regular expression pattern matching if(preg_match($pattern,$content)){ echo "Matched successfully. The second parameter contains a valid HTML connection tag string. "; }else{ echo "No valid HTML connection tag is found in the string of the second parameter. "; }?> |
In the code above, a regular expression syntax rule is used to define a pattern that matches the link tag in HTML and place it in the variable $ pattrn. Another string variable $ content. If a string contains a valid HTML connection tag, when preg_match () is used, you can search for the connection tag in the format defined by $ pattrn.
> Fixed link: http://php.ncong.com/php_course/php_zend/regex-regular.html
> For Reprinted Information, please note: ncong PHP was published in ncong PHP learning tutorial on September 10, July 31, 2014.