Commonly used functions for embedding regular expressions in PHP

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 ] )

Parameter Description:
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: PHP
In 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.com

2. 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 Description:
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:
  1. PREG_PATTERN_ORDER: By default, the results are sorted so that $matches[0] is an array of all pattern matches, $matches[1] is an array of strings matched by the subpattern in the first parenthesis, and so on
  2. PREG_SET_ORDER: sort the results so that $matches[0] is an array of the first set of matches, $matches[1] is an array of the second set of matches, and so on
  3. PREG_OFFSET_CAPTURE: If this flag is set, the associated string offset is also returned for each occurrence of a match

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.
 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324695266&siteId=291194637