PHP Regular expression preg match


In PHP applications, the regular expression is mainly used for:

  • Regular match: matches the content according to the regular expression
  • Regular Replace: Match and replace with the regular expression
  • Regular Split: Splits the string according to the regular expression

In PHP there are two types of regular expression functions, one is Perl compatible regular expression function, one is POSIX extended regular expression function. There is little difference between the two, and it is recommended to use Perl-compatible regular expression function, so the following are Perl-compatible regular expression function as an example.


delimiter

Perl-compatible regular expression function, the regular expression needs to be written in the delimiter. Any character that is not a letter, a number, or a backslash() can be used as a delimiter. Normally, we use /as a delimiter. The specific use of the following examples.


Tip

Although regular expression functions are very powerful, but if ordinary string processing functions can be done, try not to use regular expression function, because the regular expression efficiency will be much lower. For general string handling functions, see PHP string Handling.


preg_match()

the preg_match() function is used to perform a regular expression match and returns 1 if successful. Otherwise, 0 is returned.

grammar:

Int preg_match(string pattern, string subject[, array matches])
$matches[0] will contain the text that matches the entire pattern, $matches[1] will contain the text matched to the subpattern in the first captured parentheses, in order to match the text And so on.
Parameters Description
pattern regular expression
subject need to match the retrieved object
matches

Example 1:

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, the i modifier is used, so it is case-insensitive to match php in the text.


Tip

preg_match() will stop matching after the first match, if you want to achieve all the results of the match, that is, the search to the end of subject, you need to use preg_match_all() function.

Example 2, from a URL to obtain the host domain name:

// Get the host name from the URL
preg_match("/^(http: \ /\ /)?([^ \ /] +) /i","http:// www.developertut.com/index.html", $matches);
$host = $matches[2];
// Obtain the last two paragraphs from the hostname
preg_match("/[^ \. \ /] + \.[^ \. \ /] + $/", $host, $matches);
echo "domain is: {$matches[0]}";

Browser output:

Domain name: developertut.com

preg_match_all()

the preg_match_all() function is used to perform a regular expression global match, the number of successful pattern matches(possibly zero), or FALSE if the error occurs.

grammar:

Int preg_match_all(string pattern, string subject, array matches[, int flags])
Parameters Description
pattern regular expression
subject need to match the retrieved object
matches store the array of matching results
flags

Optional, specify the order in which matches are placed in matches, with the following tags:

    PREG_PATTERN_ORDER: By default, the result is sorted so that $matches[0] is an array of all pattern matches, $matches[1] is an array of substrings matched by the first parentheses, /Li>
  1. PREG_SET_ORDER: Sorts the results so that $matches[0] is the array of the first set of matches, $matches[1] is the array of the second set of matches, and so on
  2. PREG_OFFSET_CAPTURE: If this flag is set, the resulting string offset will also be returned for each occurrences of the match

The following example shows that the keyword(php) within the <pre > </pre > tag in the text is colored red.

$str = "<pre > Learning php is a happy thing. </pre > <pre > All phper needs to work together! </pre >";
$kw = "php";
preg_match_all('/<pre >([\ s \ S] *?) <\ /pre > /', $str, $mat);
$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;

Regular match British characters

Regular British characters are slightly different depending on the page encoding:

  • GBK /GB2312 encoding:[x80-xff] + or[xa1-xff] +
  • UTF-8 encoding:[x {4e00} -x {9fa5}] + /u

example:

$str = "Learning php is a happy thing.";
preg_match_all("/[x80-xff] + /", $str, $match);
// UTF-8 Use:
// preg_match_all("/[x {4e00} -x {9fa5}] + /u", $str, $match);
print_r($match);

Output:

Array
(
   [0] => Array
      (
           [0] => Learn
           [1] => is a happy thing.
        )
)



Copyright 2014-2017 by DeveloperTut.com. All Rights Reserved.
TwitterGoogle+Facebook