A detailed description of PHP regular Expressions (iii)

Source: Internet
Author: User

1, Preg_match (): the Preg_match () function is used for regular expression matching, returns 1 successfully, otherwise 0.

Syntax: int preg_match (string pattern, string subject [, array matches])

Parameters Description
Pattern Regular expressions
Subject Objects that need to be matched for retrieval
Matches Optionally, an array of matching results is stored, $matches [0] will contain text that matches the entire pattern, $matches [1] will contain the text that matches the sub-pattern in the first captured parenthesis, 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 is not found.";   }? >

Browser output:

A match was found:php

In this example, because I modifier is used, it is not case-sensitive to match PHP in the text.

Tip: Preg_match () will stop the match when the first match is successful, and 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 hostname Preg_match from URL (   "/^ (/http)?" ( [^/]+)/I "," http://blog.snsgou.com/index.html ", $matches);   $host = $matches [2]; Take the following two paragraphs from the hostname   preg_match ("/[^./]+.[ ^./]+$/", $host, $matches);   echo "domain name: {$matches [0]}";? >

Browser output:

The domain name is: snsgou.com

2, Preg_match_all (): the Preg_match_all () function is used to make a regular expression global match, successfully returns the number of times the entire pattern matches (possibly 0), and returns FALSE if an error occurs.

Syntax: int preg_match_all (string pattern, string subject, array matches [, int flags])

Parameters Description
Pattern Regular expressions
Subject Objects that need to be matched for retrieval
Matches An array that stores the matching results
Flags Optionally, specify the order in which the matching results are placed in the matches, and the tags that are available for selection are:
  1. 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 strings that match the sub-patterns in the first parenthesis, and so on
  2. Preg_set_order: Sorts 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 you set this tag, the matching result for each occurrence also returns its subordinate string offset

The following example shows that the keywords (php) in all <pre></pre> tags in the text are shown in red.

<?php    $str = "<pre> learning PHP is a happy thing. </pre><pre> all the phper need to work together! </pre> ";    $kw = "php";p reg_match_all ('/<pre> ([ss]*?) </pre>/', $str, $mat); $length = count ($mat [0]);    for ($i =0; $i < $length; $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 resolution and replace.

Syntax: Mixed preg_replace (mixed pattern, mixed replacement, mixed subject);

Return value: Mixed type data

Description: This function parses the subject string with the rule of pattern, and the string to be replaced is the parameter replacement. The return value is a mixed-type data, which is substituted for the string result.

Example: The following example returns a value of $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 =" );p rint preg_replace ($patterns, $replace, "{startdate} = 1969-6-19");

4, Preg_split (): Splits the string according to the specified rule.

Syntax: Array preg_split (string pattern, string subject, int [limit]);

return value: Array

Description: This function separates strings according to the specified rules. The return value after the cut 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, indicating the maximum value to be processed.

A detailed description of PHP regular Expressions (iii)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.