Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PHP preg_match_all() Function

PHP preg_match_all() Function

PHP Regular Expressions (PCRE)PHP Regular Expressions (PCRE)

The preg_match_all function performs a global regular expression match.

Syntax



int preg_match_all ( string $pattern , string $subject [, array &$matches [, int $flags = PREG_PATTERN_ORDER [, int $offset = 0 ]]] )

Searches subject for all matches to the regular expression given by pattern and outputs them to matches in the order specified by flag.

After the first match is found, the subsequence continues searching from the position of the last match.

Parameter Description:

  • PREG_PATTERN_ORDER: The results are ordered so that $matches[0] holds all matches of the complete pattern, $matches[1] holds all matches of the first subgroup, and so on.

  • PREG_SET_ORDER: The results are ordered so that $matches[0] contains all matches (including subgroups) from the first match, $matches[1] is an array containing all matches (including subgroups) from the second match, and so on.

  • PREG_OFFSET_CAPTURE: If this flag is passed, every match found will be returned with its offset relative to the target string.

  • 1.

    PREG_PATTERN_ORDER: The results are ordered so that $matches[0] holds all matches of the complete pattern, $matches[1] holds all matches of the first subgroup, and so on.

    2.

    PREG_SET_ORDER: The results are ordered so that $matches[0] contains all matches (including subgroups) from the first match, $matches[1] is an array containing all matches (including subgroups) from the second match, and so on.

    3.

    PREG_OFFSET_CAPTURE: If this flag is passed, every match found will be returned with its offset relative to the target string.

    Return Value

    Returns the number of complete matches (which may be 0), or FALSE if an error occurs.

    Example

    Find matches The difference between the content of the tags:

    PHP
    Title: Programming Language " ; preg_match_all ( " /(.*)<\/b>/U " , $userinfo , $pat_array ) ; print_r ( $pat_array [ 0 ] ) ; ?>

    The execution result is as follows:

    
    
    Array
    
    (
    
        [0] => PHP
    
        [1] => Programming Language
    
    )
    
    

    Find matching HTML tags (greedy):

    bold text
    click me " ; preg_match_all ( " /(<([\w]+)[^>]*>)(.*?)(<\/ \\ 2>)/ " , $html , $matches , PREG_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 execution result is as follows:

    
    
    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: 
    
    

    PHP Regular Expressions (PCRE)PHP Regular Expressions (PCRE)