What is preg_match_all in php?

❮ PHP RegExp Reference

Example

Use a regular expression to do a case-insensitive search for "w3schools" in a string:

$str = "Visit W3Schools";
$pattern = "/w3schools/i";
echo preg_match($pattern, $str);
?>

Try it Yourself »


Definition and Usage

The preg_match() function returns whether a match was found in a string.


Syntax

preg_match(pattern, input, matches, flags, offset)

Parameter Values

ParameterDescription
pattern Required. Contains a regular expression indicating what to search for
input Required. The string in which the search will be performed
matches Optional. The variable used in this parameter will be populated with an array containing all of the matches that were found
flags Optional. A set of options that change how the matches array is structured:
  • PREG_OFFSET_CAPTURE - When this option is enabled, each match, instead of being a string, will be an array where the first element is a substring containing the match and the second element is the position of the first character of the substring in the input.
  • PREG_UNMATCHED_AS_NULL - When this option is enabled, unmatched subpatterns will be returned as NULL instead of as an empty string.
offset Optional. Defaults to 0. Indicates how far into the string to begin searching. The preg_match() function will not find matches that occur before the position given in this parameter

Technical Details

Return Value:Returns 1 if a match was found, 0 if no matches were found and false if an error occurred
PHP Version:4+
Changelog:PHP 7.2 - Added the PREG_UNMATCHED_AS_NULL flag

PHP 5.3.6 - The function returns false when the offset is longer than the length of the input

PHP 5.2.2 - Named subpatterns can use the (?'name') and (? ) syntax in addition to the previous (?P)


More Examples

Example

Use PREG_OFFSET_CAPTURE to find the position in the input string in which the matches were found:

$str = "Welcome to W3Schools";
$pattern = "/w3schools/i";
preg_match($pattern, $str, $matches, PREG_OFFSET_CAPTURE);
print_r($matches);
?>

Try it Yourself »


❮ PHP RegExp Reference


(PHP 4, PHP 5, PHP 7, PHP 8)

preg_match_allPerform a global regular expression match

Description

preg_match_all(
    string $pattern,
    string $subject,
    array &$matches = null,
    int $flags = 0,
    int $offset = 0
): int|false

After the first match is found, the subsequent searches are continued on from end of the last match.

Parameters

pattern

The pattern to search for, as a string.

subject

The input string.

matches

Array of all matches in multi-dimensional array ordered according to flags.

flags

Can be a combination of the following flags (note that it doesn't make sense to use PREG_PATTERN_ORDER together with PREG_SET_ORDER):

PREG_PATTERN_ORDER

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 subpattern, and so on.

preg_match_all("|<[^>]+>(.*)]+>|U",
    
"example: this is a test

",
    
$outPREG_PATTERN_ORDER);
echo 
$out[0][0] . ", " $out[0][1] . "\n";
echo 
$out[1][0] . ", " $out[1][1] . "\n";
?>

The above example will output:

example: , 
this is a test
example: , this is a test

So, $out[0] contains array of strings that matched full pattern, and $out[1] contains array of strings enclosed by tags.

If the pattern contains named subpatterns, $matches additionally contains entries for keys with the subpattern name.

If the pattern contains duplicate named subpatterns, only the rightmost subpattern is stored in $matches[NAME].

preg_match_all(
    
'/(?J)(?foo)|(?bar)/',
    
'foo bar',
    
$matches,
    
PREG_PATTERN_ORDER
);
print_r($matches['match']);
?>

The above example will output:

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

PREG_SET_ORDER

Orders results so that $matches[0] is an array of first set of matches, $matches[1] is an array of second set of matches, and so on.

preg_match_all("|<[^>]+>(.*)]+>|U",
    
"example: this is a test",
    
$outPREG_SET_ORDER);
echo 
$out[0][0] . ", " $out[0][1] . "\n";
echo 
$out[1][0] . ", " $out[1][1] . "\n";
?>

The above example will output:

example: , example:
this is a test
, this is a test

PREG_OFFSET_CAPTURE

If this flag is passed, for every occurring match the appendant string offset (in bytes) will also be returned. Note that this changes the value of matches into an array of arrays where every element is an array consisting of the matched string at offset 0 and its string offset into subject at offset 1.

preg_match_all('/(foo)(bar)(baz)/''foobarbaz'$matchesPREG_OFFSET_CAPTURE);
print_r($matches);
?>

The above example will output:

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

        )

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

        )

    [2] => Array
        (
            [0] => Array
                (
                    [0] => bar
                    [1] => 3
                )

        )

    [3] => Array
        (
            [0] => Array
                (
                    [0] => baz
                    [1] => 6
                )

        )

)

PREG_UNMATCHED_AS_NULL

If this flag is passed, unmatched subpatterns are reported as null; otherwise they are reported as an empty string.

If no order flag is given, PREG_PATTERN_ORDER is assumed.

offset

Normally, the search starts from the beginning of the subject string. The optional parameter offset can be used to specify the alternate place from which to start the search (in bytes).

Note:

Using offset is not equivalent to passing substr($subject, $offset) to preg_match_all() in place of the subject string, because pattern can contain assertions such as ^, $ or (?<=x). See preg_match() for examples.

Return Values

Returns the number of full pattern matches (which might be zero), or false on failure.

Errors/Exceptions

If the regex pattern passed does not compile to a valid regex, an E_WARNING is emitted.

Changelog

VersionDescription
7.2.0 The PREG_UNMATCHED_AS_NULL is now supported for the $flags parameter.

Examples

Example #1 Getting all phone numbers out of some text.

preg_match_all("/\(?  (\d{3})?  \)?  (?(1)  [\-\s] ) \d{3}-\d{4}/x",
                
"Call 555-1212 or 1-800-555-1212"$phones);
?>

Example #2 Find matching HTML tags (greedy)

// The \\2 is an example of backreferencing. This tells pcre that
// it must match the second set of parentheses in the regular expression
// itself, which would be the ([\w]+) in this case. The extra backslash is
// required because the string is in double quotes.
$html "bold textclick me";preg_match_all("/(<([\w]+)[^>]*>)(.*?)(<\/\\2>)/"$html$matchesPREG_SET_ORDER);

foreach (

$matches as $val) {
    echo 
"matched: " $val[0] . "\n";
    echo 
"part 1: " $val[1] . "\n";
    echo 
"part 2: " $val[2] . "\n";
    echo 
"part 3: " $val[3] . "\n";
    echo 
"part 4: " $val[4] . "\n\n";
}
?>

The above example will output:

matched: bold text
part 1: 
part 2: b
part 3: bold text
part 4: 

matched: click me
part 1: 
part 2: a
part 3: click me
part 4: 

Example #3 Using named subpattern

$str

= <<a: 1
b: 2
c: 3
FOO;preg_match_all('/(?P\w+): (?P\d+)/'$str$matches);/* Alternative */
// preg_match_all('/(?\w+): (?\d+)/', $str, $matches);
print_r($matches);?>

The above example will output:

Array
(
    [0] => Array
        (
            [0] => a: 1
            [1] => b: 2
            [2] => c: 3
        )

    [name] => Array
        (
            [0] => a
            [1] => b
            [2] => c
        )

    [1] => Array
        (
            [0] => a
            [1] => b
            [2] => c
        )

    [digit] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
        )

    [2] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
        )

)

See Also

  • PCRE Patterns
  • preg_quote() - Quote regular expression characters
  • preg_match() - Perform a regular expression match
  • preg_replace() - Perform a regular expression search and replace
  • preg_split() - Split string by a regular expression
  • preg_last_error() - Returns the error code of the last PCRE regex execution

buuh

11 years ago

if you want to extract all {token}s from a string:

$pattern = "/{[^}]*}/";
$subject = "{token1} foo {token2} bar";
preg_match_all($pattern, $subject, $matches);
print_r($matches);
?>

output:

Array
(
    [0] => Array
        (
            [0] => {token1}
            [1] => {token2}
        )

)

Daniel Klein

7 years ago

The code that john at mccarthy dot net posted is not necessary. If you want your results grouped by individual match simply use:

preg_match_all($pattern, $string, $matches, PREG_SET_ORDER);
?>

E.g.

preg_match_all('/([GH])([12])([!?])/', 'G1? H2!', $matches); // Default PREG_PATTERN_ORDER
// $matches = array(0 => array(0 => 'G1?', 1 => 'H2!'),
//                  1 => array(0 => 'G', 1 => 'H'),
//                  2 => array(0 => '1', 1 => '2'),
//                  3 => array(0 => '?', 1 => '!'))

preg_match_all('/([GH])([12])([!?])/', 'G1? H2!', $matches, PREG_SET_ORDER);
// $matches = array(0 => array(0 => 'G1?', 1 => 'G', 2 => '1', 3 => '?'),
//                  1 => array(0 => 'H2!', 1 => 'H', 2 => '2', 3 => '!'))
?>

mnc at u dot nu

16 years ago

PREG_OFFSET_CAPTURE always seems to provide byte offsets, rather than character position offsets, even when you are using the unicode /u modifier.

phpnet at sinful-music dot com

16 years ago

Here's some fleecy code to 1. validate RCF2822 conformity of address lists and 2. to extract the address specification (the part commonly known as 'email'). I wouldn't suggest using it for input form email checking, but it might be just what you want for other email applications. I know it can be optimized further, but that part I'll leave up to you nutcrackers. The total length of the resulting Regex is about 30000 bytes. That because it accepts comments. You can remove that by setting $cfws to $fws and it shrinks to about 6000 bytes. Conformity checking is absolutely and strictly referring to RFC2822. Have fun and email me if you have any enhancements!

function mime_extract_rfc2822_address($string)
{
       
//rfc2822 token setup
       
$crlf           = "(?:\r\n)";
       
$wsp            = "[\t ]";
       
$text           = "[\\x01-\\x09\\x0B\\x0C\\x0E-\\x7F]";
       
$quoted_pair    = "(?:\\\\$text)";
       
$fws            = "(?:(?:$wsp*$crlf)?$wsp+)";
       
$ctext          = "[\\x01-\\x08\\x0B\\x0C\\x0E-\\x1F" .
                         
"!-'*-[\\]-\\x7F]";
       
$comment        = "(\\((?:$fws?(?:$ctext|$quoted_pair|(?1)))*" .
                         
"$fws?\\))";
       
$cfws           = "(?:(?:$fws?$comment)*(?:(?:$fws?$comment)|$fws))";
       
//$cfws           = $fws; //an alternative to comments
       
$atext          = "[!#-'*+\\-\\/0-9=?A-Z\\^-~]";
       
$atom           = "(?:$cfws?$atext+$cfws?)";
       
$dot_atom_text  = "(?:$atext+(?:\\.$atext+)*)";
       
$dot_atom       = "(?:$cfws?$dot_atom_text$cfws?)";
       
$qtext          = "[\\x01-\\x08\\x0B\\x0C\\x0E-\\x1F!#-[\\]-\\x7F]";
       
$qcontent       = "(?:$qtext|$quoted_pair)";
       
$quoted_string  = "(?:$cfws?\"(?:$fws?$qcontent)*$fws?\"$cfws?)";
       
$dtext          = "[\\x01-\\x08\\x0B\\x0C\\x0E-\\x1F!-Z\\^-\\x7F]";
       
$dcontent       = "(?:$dtext|$quoted_pair)";
       
$domain_literal = "(?:$cfws?\\[(?:$fws?$dcontent)*$fws?]$cfws?)";
       
$domain         = "(?:$dot_atom|$domain_literal)";
       
$local_part     = "(?:$dot_atom|$quoted_string)";
       
$addr_spec      = "($local_part@$domain)";
       
$display_name   = "(?:(?:$atom|$quoted_string)+)";
       
$angle_addr     = "(?:$cfws?<$addr_spec>$cfws?)";
       
$name_addr      = "(?:$display_name?$angle_addr)";
       
$mailbox        = "(?:$name_addr|$addr_spec)";
       
$mailbox_list   = "(?:(?:(?:(?<=:)|,)$mailbox)+)";
       
$group          = "(?:$display_name:(?:$mailbox_list|$cfws)?;$cfws?)";
       
$address        = "(?:$mailbox|$group)";
       
$address_list   = "(?:(?:^|,)$address)+";//output length of string (just so you see how f**king long it is)
       
echo(strlen($address_list) . " ");//apply expression
       
preg_match_all("/^$address_list$/", $string, $array, PREG_SET_ORDER);

        return

$array;
};
?>

chuckie

15 years ago

This is a function to convert byte offsets into (UTF-8) character offsets (this is reagardless of whether you use /u modifier:

function mb_preg_match_all($ps_pattern, $ps_subject, &$pa_matches, $pn_flags = PREG_PATTERN_ORDER, $pn_offset = 0, $ps_encoding = NULL) {
 
// WARNING! - All this function does is to correct offsets, nothing else:
  //
 
if (is_null($ps_encoding))
   
$ps_encoding = mb_internal_encoding();$pn_offset = strlen(mb_substr($ps_subject, 0, $pn_offset, $ps_encoding));
 
$ret = preg_match_all($ps_pattern, $ps_subject, $pa_matches, $pn_flags, $pn_offset);

  if (

$ret && ($pn_flags & PREG_OFFSET_CAPTURE))
    foreach(
$pa_matches as &$ha_match)
      foreach(
$ha_match as &$ha_match)
       
$ha_match[1] = mb_strlen(substr($ps_subject, 0, $ha_match[1]), $ps_encoding);
   
//
    // (code is independent of PREG_PATTER_ORDER / PREG_SET_ORDER)
return $ret;
  }
?>

fab

9 years ago

Here is a function that replaces all occurrences of a number in a string by the number--

function decremente_chaine($chaine)
    {
       
//récupérer toutes les occurrences de nombres et leurs indices
       
preg_match_all("/[0-9]+/",$chaine,$out,PREG_OFFSET_CAPTURE);
           
//parcourir les occurrences
           
for($i=0;$i<sizeof($out[0]);$i++)
            {
               
$longueurnombre = strlen((string)$out[0][$i][0]);
               
$taillechaine = strlen($chaine);
               
// découper la chaine en 3 morceaux
               
$debut = substr($chaine,0,$out[0][$i][1]);
               
$milieu = ($out[0][$i][0])-1;
               
$fin = substr($chaine,$out[0][$i][1]+$longueurnombre,$taillechaine);
                
// si c'est 10,100,1000 etc. on décale tout de 1 car le résultat comporte un chiffre de moins
                
if(preg_match('#[1][0]+$#', $out[0][$i][0]))
                 {
                    for(
$j = $i+1;$j<sizeof($out[0]);$j++)
                    {
                       
$out[0][$j][1] = $out[0][$j][1] -1;
                    }
                 }
               
$chaine = $debut.$milieu.$fin;
            }
        return
$chaine;
    }
?>

sledge NOSPAM

14 years ago

Perhaps you want to find the positions of all anchor tags.  This will return a two dimensional array of which the starting and ending positions will be returned.

function getTagPositions($strBody)
{
   
define(DEBUG, false);
   
define(DEBUG_FILE_PREFIX, "/tmp/findlinks_");preg_match_all("/<[^>]+>(.*)<\/[^>]+>/U", $strBody, $strTag, PREG_PATTERN_ORDER);
   
$intOffset = 0;
   
$intIndex = 0;
   
$intTagPositions = array();

    foreach(

$strTag[0] as $strFullTag) {
        if(
DEBUG == true) {
           
$fhDebug = fopen(DEBUG_FILE_PREFIX.time(), "a");
           
fwrite($fhDebug, $fulltag."\n");
           
fwrite($fhDebug, "Starting position: ".strpos($strBody, $strFullTag, $intOffset)."\n");
           
fwrite($fhDebug, "Ending position: ".(strpos($strBody, $strFullTag, $intOffset) + strlen($strFullTag))."\n");
           
fwrite($fhDebug, "Length: ".strlen($strFullTag)."\n\n");
           
fclose($fhDebug);
        }
       
$intTagPositions[$intIndex] = array('start' => (strpos($strBody, $strFullTag, $intOffset)), 'end' => (strpos($strBody, $strFullTag, $intOffset) + strlen($strFullTag)));
       
$intOffset += strlen($strFullTag);
       
$intIndex++;
    }
    return
$intTagPositions;
}
$strBody = 'I have lots of links on this page that I want to find the positions.';$strBody = strip_tags(html_entity_decode($strBody), '');
$intTagPositions = getTagPositions($strBody);
print_r($intTagPositions);/*****
Output:

Array (
    [0] => Array (
        [start] => 15
        [end] => 53 )
    [1] => Array (
        [start] => 62
        [end] => 99 )
    [2] => Array (
        [start] => 115
        [end] => 152 )
)
*****/

?>

bruha

14 years ago

To count str_length in UTF-8 string i use

$count = preg_match_all("/[[:print:]\pL]/u", $str, $pockets);

where
[:print:] - printing characters, including space
\pL - UTF-8 Letter
/u - UTF-8 string
other unicode character properties on http://www.pcre.org/pcre.txt

harrybarrow at mail dot ru

1 year ago

preg_match_all() and other preg_*() functions doesn't work well with very long strings, at least longer that 1Mb.
In this case case function returns FALSE and $matchers value is unpredictable, may contain some values, may be empty.
In this case workaround is pre-split long string onto parts, for instance explode() long string by some criteria and then apply preg_match_all() on each part.
Typical scenario for this case is log analysis by regular expressions.
Tested on PHP 7.2.0

spambegone at cratemedia dot com

14 years ago

I found simpleXML to be useful only in cases where the XML was extremely small, otherwise the server would run out of memory (I suspect there is a memory leak or something?). So while searching for alternative parsers, I decided to try a simpler approach. I don't know how this compares with cpu usage, but I know it works with large XML structures. This is more a manual method, but it works for me since I always know what structure of data I will be receiving.

Essentially I just preg_match() unique nodes to find the values I am looking for, or I preg_match_all to find multiple nodes. This puts the results in an array and I can then process this data as I please.

I was unhappy though, that preg_match_all() stores the data twice (requiring twice the memory), one array for all the full pattern matches, and one array for all the sub pattern matches. You could probably write your own function that overcame this. But for now this works for me, and I hope it saves someone else some time as well.

// SAMPLE XML

 
 
  PropertyID
  521897
  677208
  686037

// SAMPLE FUNCTION
function parse_xml($xml) { // GET DELIMITER (single instance)
   
$match_res = preg_match('//', $xml, $matches);
    if(!empty(
$matches[1])) {
       
$results["delimiter"] = chr($matches[1]);
    } else {
       
// DEFAULT DELIMITER
       
$results["delimiter"] = "\t";
    }
    unset(
$match_res, $matches); // GET MULTIPLE DATA NODES (multiple instances)
   
$results["data_count"] = preg_match_all("/(.*)<\/DATA>/", $xml, $matches);
   
// GET MATCHES OF SUB PATTERN, DISCARD THE REST
   
$results["data"]=$matches[1];
    unset(
$match_res, $matches); // UNSET XML TO SAVE MEMORY (should unset outside the function as well)
   
unset($xml); // RETURN RESULTS ARRAY
   
return $results;

        }

?>

meaneye at mail dot com

13 years ago

Recently I had to write search engine in hebrew and ran into huge amount of problems. My data was stored in MySQL table with utf8_bin encoding.

So, to be able to write hebrew in utf8 table you need to do
$prepared_text = addslashes(urf8_encode($text));
?>

But then I had to find if some word exists in stored text. This is the place I got stuck. Simple preg_match would not find text since hebrew doesnt work that easy. I've tried with /u and who kows what else.

Solution was somewhat logical and simple...
$db_text = bin2hex(stripslashes(utf8_decode($db_text)));
$word = bin2hex($word);$found = preg_match_all("/($word)+/i", $db_text, $matches);
?>

I've used preg_match_all since it returns number of occurences. So I could sort search results acording to that.

Hope someone finds this useful!

no at bo dot dy

12 years ago

For parsing queries with entities use:

preg_match_all("/(?:^|(?<=\&(?![a-z]+\;)))([^\=]+)=(.*?)(?:$|\&(?![a-z]+\;))/i",
 
$s, $m, PREG_SET_ORDER );
?>

john at mccarthy dot net

11 years ago

I needed a function to rotate the results of a preg_match_all query, and made this. Not sure if it exists.

function turn_array($m)
{
    for (
$z = 0;$z < count($m);$z++)
    {
        for (
$x = 0;$x < count($m[$z]);$x++)
        {
           
$rt[$x][$z] = $m[$z][$x];
        }
    }   

        return

$rt;
}
?>

Example - Take results of some preg_match_all query:

Array
(
    [0] => Array
        (
            [1] => Banff
            [2] => Canmore
            [3] => Invermere
        )

    [1] => Array
        (
            [1] => AB
            [2] => AB
            [3] => BC
        )

    [2] => Array
        (
            [1] => 51.1746254
            [2] => 51.0938416
            [3] => 50.5065193
        )

    [3] => Array
        (
            [1] => -115.5719757
            [2] => -115.3517761
            [3] => -116.0321884
        )

    [4] => Array
        (
            [1] => T1L 1B3
            [2] => T1W 1N2
            [3] => V0B 2G0
        )

)

Rotate it 90 degrees to group results as records:

Array
(
    [0] => Array
        (
            [1] => Banff
            [2] => AB
            [3] => 51.1746254
            [4] => -115.5719757
            [5] => T1L 1B3
        )

    [1] => Array
        (
            [1] => Canmore
            [2] => AB
            [3] => 51.0938416
            [4] => -115.3517761
            [5] => T1W 1N2
        )

    [2] => Array
        (
            [1] => Invermere
            [2] => BC
            [3] => 50.5065193
            [4] => -116.0321884
            [5] => V0B 2G0
        )
)

stamster at gmail dot com

6 years ago

Be careful with this pattern match and large input buffer on preg_match_* functions.

$pattern = '/\{(?:[^{}]|(?R))*\}/';preg_match_all($pattern, $buffer, $matches);
?>

if $buffer is 80+ KB in size, you'll end up with segfault!

[89396.588854] php[4384]: segfault at 7ffd6e2bdeb0 ip 00007fa20c8d67ed sp 00007ffd6e2bde70 error 6 in libpcre.so.3.13.1[7fa20c8c3000+3c000]

This is due to the PCRE recursion. This is a known bug in PHP since 2008, but it's source is not PHP itself but PCRE library.

Rasmus Lerdorf has the answer: https://bugs.php.net/bug.php?id=45735#1365812629

"The problem here is that there is no way to detect run-away regular expressions
here without huge performance and memory penalties. Yes, we could build PCRE in a
way that it wouldn't segfault and we could crank up the default backtrack limit
to something huge, but it would slow every regex call down by a lot. If PCRE
provided a way to handle this in a more graceful manner without the performance
hit we would of course use it."

ad

13 years ago

i have made up a simple function to extract a number from a string..

I am not sure how good it is, but it works.

It gets only the numbers 0-9, the "-", " ", "(", ")", "."

characters.. This is as far as I know the most widely used characters for a Phone number.

function clean_phone_number($phone) {
       if (!empty(
$phone)) {
              
//var_dump($phone);
              
preg_match_all('/[0-9\(\)+.\- ]/s', $phone, $cleaned);
               foreach(
$cleaned[0] as $k=>$v) {
                      
$ready .= $v;
               }
              
var_dump($ready);
               die;
               if (
mb_strlen($cleaned) > 4 && mb_strlen($cleaned) <=25) {
                       return
$cleaned;
               }
               else {
                       return
false;
               }
       }
       return
false;
}
?>

biziclop at vipmail dot hu

1 month ago

Sometimes you don't just want to cherry-pick the matches but need that the entire subject is made up from matching substrings, so every character of the subject is a member of a match. None of the existing preg_* function is easily applicable for this task, so I made the preg_match_entire() function.
It uses the (*MARK) syntax which is documented here: https://pcre.org/original/doc/html/pcrepattern.html#SEC27

// returns: the array of matches
// null if the string is not a repetition of the pattern
// false on error
function preg_match_entire( string $pattern, string $subject, int $flags = 0 ){
 
// Rebuild and wrap the pattern
 
$delimiter = $pattern[0];
 
$ldp       = strrpos( $pattern, $delimiter );
 
$pattern   = substr( $pattern, 1, $ldp - 1 );
 
$modifiers = substr( $pattern,    $ldp + 1 );
 
$pattern   = "{$delimiter}   \G\z (*MARK:END)   |   \G (?:{$pattern})   {$delimiter}x{$modifiers}";
 
$r = preg_match_all( $pattern, $subject, $m, PREG_SET_ORDER | $flags );
  if(
$r === false )  return false// error
 
$end = array_pop( $m );
  if(
$end === null || ! isset( $end['MARK']) || $end['MARK'] !== 'END')
    return
null// end of string not reached
 
return $m// return actual matches, may be an empty array
}// Same results:
test('#{\d+}#', '');              // []
test('#{\d+}#', '{11}{22}{33}');  // {11},{22},{33}

// Different results: preg_match_entire won't match this:

test('#{\d+}#', '{11}{}{aa}{22},{{33}}');
// preg_match_entire: null
// preg_match_all:    {11},{22},{33}
function test( $pattern, $subject ){
  echo
"pattern:           $pattern\n";
  echo
"subject:           $subject\n";
 
print_matches('preg_match_entire: ', preg_match_entire( $pattern, $subject ));
 
preg_match_all( $pattern, $subject, $matches, PREG_SET_ORDER );
 
print_matches('preg_match_all:    ', $matches );
  echo
"\n";
}
function
print_matches( $t, $m ){
  echo
$t, is_array( $m ) && $m ? implode(',', array_column( $m, 0 )) : json_encode( $m ), "\n";
}
?>

rajudec at gmail dot com

8 months ago

//Allow limited span formatting in html text$str='White
REDblue'
;

function

next_format($str)
{
    
$array=array("text-decoration-line"=>"underline","font-weight"=>"bold","font-style"=>"italic");
    foreach (
$array as $key=>$val)
    {
          if(
$str[1]==$key && $str[2]==$val)
        {
              return
$str[1].': '.$str[2].";";
        }
     }
          return
'';

  }
function

next_span($matches)
{
 
$needFormat=preg_replace_callback('/([a-z\-]+):\s*([^;]+)(;|)/ism',"next_format",$matches[2]);
  return
$matches[1].$needFormat.$matches[3];

  }
echo

preg_replace_callback(
           
"/(\)/ism",
           
"next_span",
           
$str);
?>

mojo

1 year ago

Why ('/(?:^|\s)(ABC|XYZ)(?:\s|$)/i', 'ABC  XYZ', $match) ?> finds only 'ABC'?

Because the first full match is 'ABC ' - containing the trailing space. And that space is not available for further processing.

Use lookbehind and lookahead to solve this problem: ('/(?<=^|\s)(ABC|XYZ)(?=\s|$)/i', 'ABC XYZ', $match?>

chris at ocproducts dot com

1 year ago

If PREG_OFFSET_CAPTURE is set then unmatched captures (i.e. ones with '?') will not be present in the result array. This is presumably because there is no offset, and thus the original PHP dev decided best to just leave it out.

qdinar at gmail dot com

4 years ago

when regex is for longer and shorter version of a string,
only one of that long and short versions is catched.
when regex match occurs in one position of string,
only one match is saved in matches[0] for that position.
if ? is used, regex is greedy, and catches more long version,
if | is used, most first matching variant is catched:
preg_match_all('/ab|abc/','abc',$m);
var_dump($m);
preg_match_all('/abc?/','abc',$m);
var_dump($m);
?>
['ab', 'abc'] in $m[0] for both can be expected, but it is not so,
actually they output [['ab']] and [['abc']]:
array(1) {
  [0]=>
  array(1) {
    [0]=>
    string(2) "ab"
  }
}
array(1) {
  [0]=>
  array(1) {
    [0]=>
    string(3) "abc"
  }
}

fseverin at free dot fr

9 years ago

As I intended to create for my own purpose a clean PHP class to act on XML files, combining the use of DOM and simplexml functions, I had that small problem, but very annoying, that the offsets in a path is not numbered the same in both.

That is to say, for example, if i get a DOM xpath object it appears like:
/ANODE/ANOTHERNODE/SOMENODE[9]/NODE[2]
and as a simplexml object would be equivalent to:
ANODE->ANOTHERNODE->SOMENODE[8]->NODE[1]

So u see what I mean? I used preg_match_all to solve that problem, and finally I got this after some hours of headlock (as I'm french the names of variables are in French sorry), hoping it could be useful to some of you:

function decrease_string($string)
    {
       
/* retrieve all occurrences AND offsets of numbers in the original string: */ preg_match_all("/[0-9]+/",$chaine,$out,PREG_OFFSET_CAPTURE);
            for(
$i=0;$i<sizeof($out[0]);$i++)
            {
               
$longueurnombre = strlen((string)$out[0][$i][0]);
               
$taillechaine = strlen($chaine);
               
// cut the string in 3 pieces
               
$debut = substr($chaine,0,$out[0][$i][1]);
               
$milieu = ($out[0][$i][0])-1;
               
$fin = substr($chaine,$out[0][$i][1]+$longueurnombre,$taillechaine);
                
/* if it's 10,100,1000, the problem is that the string gets shorter and it shifts all the offsets, so we have to decrease them of 1 */
                
if(preg_match('#[1][0]+$#', $out[0][$i][0]))
                 {
                    for(
$j = $i+1;$j<sizeof($out[0]);$j++)
                    {
                       
$out[0][$j][1] = $out[0][$j][1] -1;
                    }
                 }
               
$chaine = $debut.$milieu.$fin;
            }
        return
$chaine;
    }
?>

marc

10 years ago

Better use preg_replace to convert text in a clickable link with tag

$html = preg_replace('"\b(http://\S+)"', '$1', $text);

royaltm75 at gmail dot com

12 years ago

I have received complains, that my html2a() code (see below) doesn't work in some cases.
It is however not the problem with algorithm or procedure, but with PCRE recursive stack limits.

If you use recursive PCRE (?R) you should remember to increase those two ini settings:

ini_set('pcre.backtrack_limit', 10000000);
ini_set('pcre.recursion_limit', 10000000);

But be warned: (from php.ini)

;Please note that if you set this value to a high number you may consume all
;the available process stack and eventually crash PHP (due to reaching the
;stack size limit imposed by the Operating System).

I have written this example mainly to demonstrate the power of PCRE LANGUAGE, not the power of it's implementation  :)

But if you like it, use it, of course on your own risk.

elyknosrac at gmail dot com

13 years ago

Using preg_match_all I made a pretty handy function.

function reg_smart_replace($pattern, $replacement, $subject, $replacementChar = "$$$", $limit = -1)
{
    if (!
$pattern || ! $subject || ! $replacement ) { return false; } $replacementChar = preg_quote($replacementChar); preg_match_all ( $pattern, $subject, $matches);

        if (

$limit > -1) {
        foreach (
$matches as $count => $value )
        {
            if (
$count + 1 > $limit ) { unset($matches[$count]); }
        }
    }
    foreach (
$matches[0] as $match) {
       
$rep = ereg_replace($replacementChar, $match, $replacement);
       
$subject = ereg_replace($match, $rep, $subject);
    }

        return

$subject;
}
?>

This function can turn blocks of text into clickable links or whatever.  Example:

reg_smart_replace(EMAIL_REGEX, '$$$', $description)
?>
will turn all email addresses into actual links.

Just substitute $$$ with the text that will be found by the regex.  If you can't use $$$ then use the 4th parameter $replacementChar

MonkeyMan

13 years ago

Here is a way to match everything on the page, performing an action for each match as you go. I had used this idiom in other languages, where its use is customary, but in PHP it seems to be not quite as common.

function custom_preg_match_all($pattern, $subject)
{
   
$offset = 0;
   
$match_count = 0;
    while(
preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE, $offset))
    {
       
// Increment counter
       
$match_count++;// Get byte offset and byte length (assuming single byte encoded)
       
$match_start = $matches[0][1];
       
$match_length = strlen(matches[0][0]);// (Optional) Transform $matches to the format it is usually set as (without PREG_OFFSET_CAPTURE set)
       
foreach($matches as $k => $match) $newmatches[$k] = $match[0];
       
$matches = $new_matches;// Your code here
       
echo "Match number $match_count, at byte offset $match_start, $match_length bytes long: ".$matches[0]."\r\n";// Update offset to the end of the match
       
$offset = $match_start + $match_length;
    }

    return

$match_count;
}
?>

Note that the offsets returned are byte values (not necessarily number of characters) so you'll have to make sure the data is single-byte encoded. (Or have a look at paolo mosna's strByte function on the strlen manual page).
I'd be interested to know how this method performs speedwise against using preg_match_all and then recursing through the results.

dolbegraeb

14 years ago

please note, that the function of "mail at SPAMBUSTER at milianw dot de" can result in invalid xhtml in some cases. think i used it in the right way but my result is sth like this:

What is preg_match_all in php?
foo foo foo foo

correct me if i'm wrong.
i'll see when there's time to fix that. -.-

matt at lvl99 dot com

6 years ago

I had been crafting and testing some regexp patterns online using the tools Regex101 and a `preg_match_all()` tester and found that the regexp patterns I wrote worked fine on them, just not in my code.

My problem was not double-escaping backslash characters:

// Input test
$input = "\"something\",\"something here\",\"some\nnew\nlines\",\"this is the end\"";// Work with online regexp testers, doesn't work in PHP
preg_match_all( "/(?:,|^)(?, $input, $matches );/*
Outputs: NULL
*/

// Works with online regexp testers, does work in PHP

preg_match_all( "/(?:,|^)(?, $input, $matches );/*
Outputs:
array(2) {
  [0]=>
  array(4) {
    [0]=>
    string(11) ""something""
    [1]=>
    string(17) ","something here""
    [2]=>
    string(17) ","some
new
lines""
    [3]=>
    string(18) ","this is the end""
  }
  [1]=>
  array(4) {
    [0]=>
    string(9) "something"
    [1]=>
    string(14) "something here"
    [2]=>
    string(14) "some
new
lines"
    [3]=>
    string(15) "this is the end"
  }
}
*/
?>

phektus at gmail dot com

15 years ago

If you'd like to include DOUBLE QUOTES on a regular expression for use with preg_match_all, try ESCAPING THRICE, as in: \\\"

For example, the pattern:
'/

[\s\w\/<>=\\\"]*<\/table>/'

Should be able to match:



a
b


.. with all there is under those table tags.

I'm not really sure why this is so, but I tried just the double quote and one or even two escape characters and it won't work. In my frustration I added another one and then it's cool.

mr davin

15 years ago

// Returns an array of strings where the start and end are found
   
function findinside($start, $end, $string) {
       
preg_match_all('/' . preg_quote($start, '/') . '([^\.)]+)'. preg_quote($end, '/').'/i', $string, $m);
        return
$m[1];
    }
$start = "mary has";
   
$end = "lambs.";
   
$string = "mary has 6 lambs. phil has 13 lambs. mary stole phil's lambs. now mary has all the lambs.";$out = findinside($start, $end, $string);print_r ($out);/* Results in
(
    [0] =>  6
    [1] =>  all the
)
*/
?>

vojjov dot artem at ya dot ru

7 years ago

// Here is function that allows you to preg_match_all array of patters

function getMatches($pattern, $subject) {
    $matches = array();

    if (is_array($pattern)) {
        foreach ($pattern as $p) {
            $m = getMatches($p, $subject);

            foreach ($m as $key => $match) {
                if (isset($matches[$key])) {
                    $matches[$key] = array_merge($matches[$key], $m[$key]);   
                } else {
                    $matches[$key] = $m[$key];
                }
            }
        }
    } else {
        preg_match_all($pattern, $subject, $matches);
    }

    return $matches;
}

$patterns = array(
    '/(.*?)<\/span>/',
    '/(.*?)<\/a>/'
);

$html = 'some text';
$html .= 'some text in another span';
$html .= '
here is the link';
$html .= '

address is here
';
$html .= 'here is one more span';

$matches = getMatches($patterns, $html);

print_r($matches); // result is below

/*
Array
(
    [0] => Array
        (
            [0] => some text
            [1] => some text in another span
            [2] => here is one more span
            [3] => here is the link
        )

    [1] => Array
        (
            [0] => some text
            [1] => some text in another span
            [2] => here is one more span
            [3] => here is the link
        )

)
*/

satyavvd at ymail dot com

10 years ago

Extract fields out of csv string : ( since before php5.3 you can't use str_getcsv function )
Here is the regex :

$csvData

= <<10,'20',"30","'40","'50'","\"60","70,80","09\\/18,/\"2011",'a,sdfcd'
EOF $reg = <</
    (
        (
            ([\'\"])
            (
               (
                [^\'\"]
                |
                (\\\\.)
               )*
            )
            (\\3)
            |
            (
                [^,]
                |
                (\\\\.)
            )*
    ),)
    /x
EOF; preg_match_all($reg,$csvData,$matches); // to extract csv fields
print_r($matches[2]);
?>

royaltm75 at NOSPAM dot gmail dot com

13 years ago

The power of pregs is limited only by your *imagination* :)
I wrote this html2a() function using preg recursive match (?R) which provides quite safe and bulletproof html/xml extraction:
function html2a ( $html ) {
  if ( !
preg_match_all( '
@
\<\s*?(\w+)((?:\b(?:\'[^\']*\'|"[^"]*"|[^\>])*)?)\>
((?:(?>[^\<]*)|(?R))*)
\<\/\s*?\\1(?:\b[^\>]*)?\>
|\<\s*(\w+)(\b(?:\'[^\']*\'|"[^"]*"|[^\>])*)?\/?\>
@uxis'
, $html = trim($html), $m, PREG_OFFSET_CAPTURE | PREG_SET_ORDER) )
    return
$html;
 
$i = 0;
 
$ret = array();
  foreach (
$m as $set) {
    if (
strlen( $val = trim( substr($html, $i, $set[0][1] - $i) ) ) )
     
$ret[] = $val;
   
$val = $set[1][1] < 0
     
? array( 'tag' => strtolower($set[4][0]) )
      : array(
'tag' => strtolower($set[1][0]), 'val' => html2a($set[3][0]) );
    if (
preg_match_all( '
/(\w+)\s*(?:=\s*(?:"([^"]*)"|\'([^\']*)\'|(\w+)))?/usix
'
, isset($set[5]) && $set[2][1] < 0
 
? $set[5][0]
  :
$set[2][0]
  ,
$attrs, PREG_SET_ORDER ) ) {
      foreach (
$attrs as $a) {
       
$val['attr'][$a[1]]=$a[count($a)-1];
      }
    }
   
$ret[] = $val;
   
$i = $set[0][1]+strlen( $set[0][0] );
  }
 
$l = strlen($html);
  if (
$i < $l )
    if (
strlen( $val = trim( substr( $html, $i, $l - $i ) ) ) )
     
$ret[] = $val;
  return
$ret;
}
?>

Now let's try it with this example: (there are some really nasty xhtml compliant bugs, but ... we shouldn't worry)

$html = <<some leftover text...
     < DIV class=noCompliant style = "text-align:left;" >
... and some other ...
< dIv > < empty> 
 

This is yet another text
     that wasn't compliant too...
    


this one is better but we don't care anyway


   
end of paragraph

      some trailing text
EOT;$a = html2a($html);
//now we will make some neat html out of it
echo a2html($a);

function

a2html ( $a, $in = "" ) {
  if (
is_array($a) ) {
   
$s = "";
    foreach (
$a as $t)
      if (
is_array($t) ) {
       
$attrs="";
        if ( isset(
$t['attr']) )
          foreach(
$t['attr'] as $k => $v )
           
$attrs.=" ${k}=".( strpos( $v, '"' )!==false ? "'$v'" : "\"$v\"" );
       
$s.= $in."<".$t['tag'].$attrs.( isset( $t['val'] ) ? ">\n".a2html( $t['val'], $in."  " ).$in.".$t['tag'] : "/" ).">\n";
      } else
       
$s.= $in.$t."\n";
  } else {
   
$s = empty($a) ? "" : $in.$a."\n";
  }
  return
$s;
}
?>
This produces:
some leftover text...

  ... and some other ...
 

   
   

   


      This is yet another text
     

      that wasn't
     
        compliant
     

      too...
     

   


   

      this one is better but we don't care anyway
   

   


     
      end of paragraph
   


 


some trailing text

DarkSide

8 years ago

This is very useful to combine matches:
$a = array_combine($matches[1], $matches[2]);

avengis at gmail dot com

12 years ago

The next function works with almost any complex xml/xhtml string

/**
* Find and close unclosed xml tags
**/
function close_tags($text) {
   
$patt_open    = "%((?\s]+(?=>|[\s]+[^>]*[^/]>)(?!/>))%";
   
$patt_close    = "%((?<=]+)(?=>))%";
    if (
preg_match_all($patt_open,$text,$matches))
    {
       
$m_open = $matches[1];
        if(!empty(
$m_open))
        {
           
preg_match_all($patt_close,$text,$matches2);
           
$m_close = $matches2[1];
            if (
count($m_open) > count($m_close))
            {
               
$m_open = array_reverse($m_open);
                foreach (
$m_close as $tag) $c_tags[$tag]++;
                foreach (
$m_open as $k => $tag)    if ($c_tags[$tag]--<=0) $text.='.$tag.'>';
            }
        }
    }
    return
$text;
}
?>

What is the use of preg match?

The preg_match() function returns whether a match was found in a string.

What is PHP regex?

In PHP, regular expressions are strings composed of delimiters, a pattern and optional modifiers. $exp = "/w3schools/i"; In the example above, / is the delimiter, w3schools is the pattern that is being searched for, and i is a modifier that makes the search case-insensitive.

What is Preg_replace function in PHP?

The preg_replace() function returns a string or array of strings where all matches of a pattern or list of patterns found in the input are replaced with substrings. There are three different ways to use this function: 1. One pattern and a replacement string.

What does preg match return?

preg_match() returns 1 if the pattern matches given subject , 0 if it does not, or false on failure. This function may return Boolean false , but may also return a non-Boolean value which evaluates to false .