There are four commonly used functions for embedding regular expressions in PHP:
1. preg_match(): The preg_match() function is used for regular expression matching, and returns 1 if successful, otherwise it returns 0.
Syntax: int preg_match( string pattern, string subject [, array matches ] )
Example 1:
Tip: preg_match() will stop matching after the first successful match. If you want to match all results, that is, search to the end of subject, you need to use the preg_match_all() function.
Example 2, get the host domain name from a URL:
Syntax: int preg_match_all( string pattern, string subject, array matches [, int flags ] )
The following example demonstrates displaying all keywords (php) within <pre></pre> tags in the text in red.
1. preg_match(): The preg_match() function is used for regular expression matching, and returns 1 if successful, otherwise it returns 0.
Syntax: int preg_match( string pattern, string subject [, array matches ] )
parameter | illustrate |
pattern | regular expression |
subject | Objects that need to be matched to retrieve |
matches | Optional, an array to store the match results, $matches[0] will contain the text that matched the entire pattern, $matches[1] will contain the text that matched the first captured subpattern in parentheses, and so on |
Example 1:
<?php if(preg_match("/php/i", "PHP is the web scripting language of choice.", $matches)) { print "A match was found:". $matches[0]; } else { print "A match was not found."; } ?>Browser output:
A match was found: PHPIn this example, since the i modifier is used, it will be case-insensitive to match php in the text.
Tip: preg_match() will stop matching after the first successful match. If you want to match all results, that is, search to the end of subject, you need to use the preg_match_all() function.
Example 2, get the host domain name from a URL:
<?php // Get the hostname from the URL preg_match("/^(http://)?([^/]+)/i","http://www.5idev.com/index.html", $matches); $host = $matches[2]; // Get the last two paragraphs from the hostname preg_match("/[^./]+.[^./]+$/", $host, $matches); echo "The domain name is: {$matches[0]}"; ?>Browser output:
The domain name is: 5idev.com2. preg_match_all(): The preg_match_all() function is used for global regular expression matching, and returns the number of times the entire pattern is matched successfully (may be zero), and returns FALSE if there is an error.
Syntax: int preg_match_all( string pattern, string subject, array matches [, int flags ] )
parameter | illustrate |
---|---|
pattern | regular expression |
subject | Objects that need to be matched to retrieve |
matches | Array to store matching results |
flags | Optional, specifies the order in which the matching results are placed in matches. The optional tags are:
|
The following example demonstrates displaying all keywords (php) within <pre></pre> tags in the text in red.
<?php $str = "<pre>Learning php is a joy.</pre><pre>All phpers need to work together!</pre>"; $kw = "php"; preg_match_all('/<pre>([sS]*?)</pre>/',$str,$mat); for($i=0;$i<count($mat[0]);$i++) { $ mat [0] [$ i] = $ mat [1] [$ i]; $mat[0][$i] = str_replace($kw, '<span style="color:#ff0000">'.$kw.'</span>', $mat[0][$i]); $str = str_replace($mat[1][$i], $mat[0][$i], $str); } echo $str; ?> 3. preg_replace(): String comparison analysis and replacement. 语法: mixed preg_replace(mixed pattern, mixed replacement, mixed subject); Return value: mixed type data Description: This function parses and compares the string subject according to the rules of pattern, and the string to be replaced is the parameter replacement. The return value is mixed type data, which is the string result after substitution. Example: The return value of the following example is $startDate = 6/19/1969 $patterns = array("/(19|20\d{2})-(\d{1,2})-(\d{1,2})/", "/^\s*{(\w+)}\s*=/"); $replace = array("\\3/\\4/\\1", "$\\1 ="); print preg_replace($patterns, $replace, "{startDate} = 1969-6-19"); 4. preg_split(): Split the string according to the specified rules. 语法: array preg_split(string pattern, string subject, int [limit]); Return value: array Description: This function can separate strings according to the specified rules. The return value after cutting is an array variable. The parameter pattern is the specified rule string, the parameter subject is the string to be processed, and the parameter limit can be omitted, which means the most acceptable values to be processed.