int preg_match_all ( string pattern, string subject, array matches [, int flags] )5 E: J4 G; b3 X4 g5 {+ e
3 Y& ?8 T- q, A- `" h) @8 O
subject 銝剜蝝Xpattern 蝏箇甇閬銵函內撘寥摰嫣蒂撠蝏隞 flags 摰摨曉 matches 銝准 蝝W啁洵銝銝芸寥銋嚗乩亦蝝W銝銝寥怠偏憪 flags 臭誑臭璅閮蝯嚗瘜冽 PREG_PATTERN_ORDER PREG_SET_ORDER 韏瑚冽瓷蝢抬嚗
4 N, Q8 Y j* M% m. ZPREG_PATTERN_ORDER撠蝯摨雿 $matches[0] 箏券冽芋撘寥摮銝莎$matches[1]箇洵銝砍瑚葉摮璅∪寥摮銝莎隞交迨憿具 - <?php
0 ?, N- X4 l1 D - preg_match_all ("|<[^>]+>(.*)</[^>]+>|U",
1 t! X ]* S! T) Z9 H - "<b>example: </b><div align=left>this is a test</div>",' K+ t3 Y A% v& m. [9 z
- $out, PREG_PATTERN_ORDER);
$ v Y Z; I" M0 c9 Q - print $out[0][0].", ".$out[0][1]."\n";& }- o7 R8 ^- T; W
- print $out[1][0].", ".$out[1][1]."\n";$ b$ p# [7 d* k5 d9 [
- ?>
銴鋆賭誨蝣 # N* w" w# W* v- U* h+ O4 X
砌撠颲綽 - <b>example: </b>, <div align=left>this is a test</div>
% t7 Y6 P% }+ q% O* t - example: , this is a test
銴鋆賭誨蝣
( k; s( M1 x+ H8 M3 ], g! A甇歹$out[0] 怠寥港葵璅∪摮銝莎$out[1] 思撠HTML 璅閮銋摮銝脯
1 w/ w8 I" k; o V: i# ~+ I3 q# U: {' h6 N5 v3 q. a4 S
0 L8 E+ @+ E h6 b$ yPREG_SET_ORDER撖寧摨雿 $matches[0] 銝箇洵銝蝏寥憿寧啁嚗$matches[1] 銝箇洵鈭蝏寥憿寧啁嚗隞交迨蝐餅具 - <?php, S: X# J! X2 F `1 `! a, p. }
- preg_match_all ("|<[^>]+>(.*)</[^>]+>|U",
" `- B$ m* O- e/ e, I- m: c+ I, x - "<b>example: </b><div align=left>this is a test</div>",
6 m2 c. x4 J. G5 V - $out, PREG_SET_ORDER); l" v) m2 X. @+ Q0 Y6 {! H- V
- print $out[0][0].", ".$out[0][1]."\n";! y: q _: C4 y$ R" E$ D8 C
- print $out[1][0].", ".$out[1][1]."\n";
% d6 p" W2 m ] - ?>
銴鋆賭誨蝣
& K9 f5 U! a. w0 I6 e砌撠颲綽 - <b>example: </b>, example:* b2 i/ {* ?/ i6 A3 v7 W3 ]& g( K5 M
- <div align=left>this is a test</div>, this is a test" K, O% J5 [1 E9 T/ b) a
銴鋆賭誨蝣 % I. `$ R+ k( K+ _& q
砌銝哨$matches[0] 舐洵銝蝏寥蝏嚗$matches[0][0] 怠寥港葵璅∪穿$matches[0][1] 怠寥蝚砌銝芸璅∪穿隞交迨蝐餅具瘀$matches[1] 舐洵鈭蝏寥蝏嚗蝑蝑 PREG_OFFSET_CAPTURE憒霈曉祆霈堆撖寞銝芸箇啁寥蝏銋嗉園撅摮蝚虫葡蝘駁瘜冽餈孵鈭餈啁潘雿踹嗡葉瘥銝芸銋臭銝芣啁嚗嗡葉蝚砌憿嫣蛹寥摮蝚虫葡嚗蝚砌憿嫣蛹嗅 subject 銝剔蝘駁祆霈啗 PHP 4.3.0 韏瑕舐具
6 g3 ]( j+ T" Q5 s- G0 d6 ]7 V' A# k/ c6 w# }) ~
憒瘝⊥蝏箸霈堆摰銝 PREG_PATTERN_ORDER 餈港葵璅∪寥甈⊥堆航賭蛹塚嚗憒粹餈 FALSE
. F2 s2 ^4 t: S靘摮 1. 隞砌葉敺菔瑞 - <?php
" f" R# V; [) \ - preg_match_all ("/\(? (\d{3})? \)? (?(1) [\-\s] ) \d{3}-\d{4}/x",
) @+ O w4 p. _) \ - "Call 555-1212 or 1-800-555-1212 FREE", $phones);' N4 z4 U. z# H' h
- ?>
銴鋆賭誨蝣靘摮 2. 蝝W寥 HTML 霈堆greedy嚗 - <?php
3 G) @( ?2 N! ^ {) p - // \\2 臭銝芷撘函靘摮嚗嗅 PCRE 銝剔思) ~2 \2 H# V6 I! G' y" f, d0 w5 r
- // 敹憿餃寥甇銵刻噢撘祈澈銝剔洵鈭蝏砍瑕摰對砌銝
& [9 ]+ c2 l5 [4 Q - // 撠望 ([\w]+)銝箏蝚虫葡典撘瑚葉嚗隞仿閬
+ m: i5 c T3 u( S1 R0 }: e - // 憭銝銝芸蝥踴/ G1 N- u$ v3 G- k* u. M7 C3 p
- $html = "<b>bold text</b><a href=howdy.html>click me</a>";; V3 e# P. j( P5 s9 g; T
- % F, f+ P) I) q' G0 N0 a
- preg_match_all ("/(<([\w]+)[^>]*>)(.*)(<\/\\2>)/", $html, $matches);% c0 O) K G2 f* g
1 U( p* r" B- @: b! M& v5 S- for ($i=0; $i< count($matches[0]); $i++) {/ X' ]& R; \0 I$ {5 L! r
- echo "matched: ".$matches[0][$i]."\n";
- u5 `/ x- R U- J q* I - echo "part 1: ".$matches[1][$i]."\n";
! C) }8 D$ z7 e F, g* D - echo "part 2: ".$matches[3][$i]."\n";4 [3 A6 z' t- `9 r* I B! I7 s/ ~# U
- echo "part 3: ".$matches[4][$i]."\n\n";2 ]# g+ j5 i7 }; P/ S
- }
. U: Q4 ]; u' H0 F - ?>
銴鋆賭誨蝣砌撠颲綽 - matched: <b>bold text</b>; N% x* [+ v* g. d! a) \. P
- part 1: <b>
5 ?) q; C5 a) _% E) j - part 2: bold text' s# i6 k5 x5 g8 m- \
- part 3: </b>
: e7 F6 y9 X% r& Y2 p8 o; ]/ q5 E - ; x- N( h) u3 N' a# h
- matched: <a href=howdy.html>click me</a>+ B" `6 |5 z7 ~, E( b1 [
- part 1: <a href=howdy.html>
6 w# W5 w h' |' y) Y7 X - part 2: click me
+ X. O/ E, ^" m7 `% A' G - part 3: </a>
銴鋆賭誨蝣
( j% i8 W }1 ~5 [' L. l$ b, X( {
|
|