Biểu thức chính quy trong php

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

Bạn đang xem: Biểu thức chính quy trong php

Searches subject for all matches to lớn the regular expression given in pattern và puts them in matches in the order specified by flags.

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


Parameters

pattern The pattern khổng lồ tìm kiếm for, as a string.

subject

The input string.

matches

Array of all matches in multi-dimensional array ordered according khổng lồ 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, và so on.


So, $out<0> contains array of strings that matched full pattern, & $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.
("/(?J)(?foo)|(?bar)/","foobar",$matches,PREG_PATTERN_ORDER);print_r($matches<"match">);?>

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

PREG_OFFSET_CAPTURE

If this flag is passed, for every occurring match the appendant string offset (in bytes) will also be returned. cảnh báo that this changes the value of matches into lớn an array of arrays where every element is an array consisting of the matched string at offphối 0 and its string offset into subject at offset 1.

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 ) ))

Xem thêm: Hướng Dẫn Sử Dụng Onekey Ghost Để Tạo Và Bung File Ghost Nhanh Chóng

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 tìm kiếm starts from the beginning of the subject string. The optional parameter offset can be used lớn specify the alternate place from which lớn start the search (in bytes).

Note: Using offphối is not equivalent lớn passing substr($subject, $offset) to preg_match_all() in place of the subject string, because pattern can contain assertions such as ^, $ or (?preg_match() for examples.


Return Values

Returns the number of full pattern matches (which might be zero), or false if an error occurred.


Changelog

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


preg_match_all("/(?(d3)?)?(?(1)<-s>)d3-d4/x","Call555-1212or1-800-555-1212",$phones);?>
//The\2isanexampleofbackreferencing.Thistellspcrethat//itmustmatchthesecondsetofparenthesesintheregularexpression//itself,whichwouldbethe(+)inthiscase.Theextrabackslashis//requiredbecausethestringisindoublequotes.$html="boldtextclickme";preg_match_all("/(>*>)(.*?)()/",$html,$matches,PREG_SET_ORDER);foreach($matchesas$val)echo"matched:".$val<0>." ";echo"part1:".$val<1>." ";echo"part2:".$val<2>." ";echo"part3:".$val<3>." ";echo"part4:".$val<4>." ";?>

matched: bold textpart 1: part 2: bpart 3: bold textpart 4: matched: cliông xã mepart 1: part 2: apart 3: cliông xã mepart 4:
$str=a:1b:2c:3FOO;preg_match_all("/(?Pw+):(?Pd+)/",$str,$matches);/*Alternative*///preg_match_all("/(?w+):(?d+)/",$str,$matches);print_r($matches);?>

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

*
"; $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 sầu to comments $atext = ""; $atom = "(?:$cfws?$atext+$cfws?)"; $dot_atom_text = "(?:$atext+(?:\.$atext+)*)"; $dot_atom = "(?:$cfws?$dot_atom_text$cfws?)"; $qtext = "<\x01-\x08\x0B\x0C\x0E-\x1F!#-<\>-\x7F>"; $qnội dung = "(?:$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?)"; $tên miền = "(?:$dot_atom|$domain_literal)"; $local_part = "(?:$dot_atom|$quoted_string)"; $addr_spec = "($local_part