Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP preg_match and preg_match_all functions

What do the preg_match() and preg_match_all() functions do and how can I use them?

like image 858
sikas Avatar asked Nov 03 '10 15:11

sikas


People also ask

What is Preg_match_all function in PHP?

PHP preg_match_all() Function The preg_match_all() function returns the number of matches of a pattern that were found in a string and populates a variable with the matches that were found.

What is the purpose of Preg_match () regular expression in PHP?

The preg_match() function will tell you whether a string contains matches of a pattern.

What is pattern matching in PHP?

preg_match() in PHP – this function is used to perform pattern matching in PHP on a string. It returns true if a match is found and false if a match is not found. preg_split() in PHP – this function is used to perform a pattern match on a string and then split the results into a numeric array.


Video Answer


3 Answers

preg_match stops looking after the first match. preg_match_all, on the other hand, continues to look until it finishes processing the entire string. Once match is found, it uses the remainder of the string to try and apply another match.

http://php.net/manual/en/function.preg-match-all.php

like image 166
romaninsh Avatar answered Oct 17 '22 18:10

romaninsh


Both preg_match and preg_match_all functions in PHP use Perl compatible regular expressions.

You can watch this series to fully understand Perl compatible regular expressions: https://www.youtube.com/watch?v=GVZOJ1rEnUg&list=PLfdtiltiRHWGRPyPMGuLPWuiWgEI9Kp1w

preg_match($pattern, $subject, &$matches, $flags, $offset)

The preg_match function is used to search for a particular $pattern in a $subject string and when the pattern is found the first time, it stops searching for it. It outputs matches in the $matches, where $matches[0] will contain the text that matched the full pattern, $matches[1] will have the text that matched the first captured parenthesized sub-pattern, and so on.

Example of preg_match()

<?php
preg_match(
    "|<[^>]+>(.*)</[^>]+>|U",
    "<b>example: </b><div align=left>this is a test</div>",
    $matches
);

var_dump($matches);

Output:

array(2) {
  [0]=>
  string(16) "<b>example: </b>"
  [1]=>
  string(9) "example: "
}

preg_match_all($pattern, $subject, &$matches, $flags)

The preg_match_all function searches for all the matches in a string and outputs them in a multi-dimensional array ($matches) ordered according to $flags. When no $flags value is passed, it orders results so that $matches[0] is an array of full pattern matches, $matches[1] is an array of strings matched by the first parenthesized sub-pattern, and so on.

Example of preg_match_all()

<?php
preg_match_all(
    "|<[^>]+>(.*)</[^>]+>|U",
    "<b>example: </b><div align=left>this is a test</div>",
    $matches
);

var_dump($matches);

Output:

array(2) {
  [0]=>
  array(2) {
    [0]=>
    string(16) "<b>example: </b>"
    [1]=>
    string(36) "<div align=left>this is a test</div>"
  }
  [1]=>
  array(2) {
    [0]=>
    string(9) "example: "
    [1]=>
    string(14) "this is a test"
  }
}
like image 22
Sumit Avatar answered Oct 17 '22 17:10

Sumit


A concrete example:

preg_match("/find[ ]*(me)/", "find me find   me", $matches):
$matches = Array(
    [0] => find me
    [1] => me
)

preg_match_all("/find[ ]*(me)/", "find me find   me", $matches):
$matches = Array(
    [0] => Array
        (
            [0] => find me
            [1] => find   me
        )

    [1] => Array
        (
            [0] => me
            [1] => me
        )
)

preg_grep("/find[ ]*(me)/", ["find me find    me", "find  me findme"]):
$matches = Array
(
    [0] => find me find    me
    [1] => find  me findme
)
like image 8
Rebecca Avatar answered Oct 17 '22 17:10

Rebecca