int preg_match_all ( string pattern, string subject, array matches [, int flags] )& x f: Y% j1 X! O
. O; L# q; \. ^8 a, {; c V9 h
subject 銝剜蝝Xpattern 蝏箇甇閬銵函內撘寥摰嫣蒂撠蝏隞 flags 摰摨曉 matches 銝准 蝝W啁洵銝銝芸寥銋嚗乩亦蝝W銝銝寥怠偏憪 flags 臭誑臭璅閮蝯嚗瘜冽 PREG_PATTERN_ORDER PREG_SET_ORDER 韏瑚冽瓷蝢抬嚗
4 {9 B0 C4 b$ q$ r2 q! u8 UPREG_PATTERN_ORDER撠蝯摨雿 $matches[0] 箏券冽芋撘寥摮銝莎$matches[1]箇洵銝砍瑚葉摮璅∪寥摮銝莎隞交迨憿具 - <?php
- G' z: E0 \: v/ d - preg_match_all ("|<[^>]+>(.*)</[^>]+>|U",
8 s7 }5 p. H Y* A - "<b>example: </b><div align=left>this is a test</div>",) X& B6 o4 F9 p) E/ Q
- $out, PREG_PATTERN_ORDER);
1 j/ p0 o6 U" G0 G% r; R' L: [ - print $out[0][0].", ".$out[0][1]."\n";
/ o3 y3 p2 f- T* x& p! p7 z- C - print $out[1][0].", ".$out[1][1]."\n";
4 H! m5 j) ]( f) c. N* I* G - ?>
銴鋆賭誨蝣 8 p5 G! N+ p: q: B1 [
砌撠颲綽 - <b>example: </b>, <div align=left>this is a test</div>
8 t# W4 [* p8 U - example: , this is a test
銴鋆賭誨蝣
+ ?2 V8 |$ f+ c: a- V, d) Q3 t- S甇歹$out[0] 怠寥港葵璅∪摮銝莎$out[1] 思撠HTML 璅閮銋摮銝脯
) V$ u% ]- R+ E' a/ |
4 F& b: ]6 D o) T: ]
- L% y$ b. P" gPREG_SET_ORDER撖寧摨雿 $matches[0] 銝箇洵銝蝏寥憿寧啁嚗$matches[1] 銝箇洵鈭蝏寥憿寧啁嚗隞交迨蝐餅具 - <?php
3 I# P; s2 A! V5 Q - preg_match_all ("|<[^>]+>(.*)</[^>]+>|U",* h" n, w! L' m2 {1 o5 h: j
- "<b>example: </b><div align=left>this is a test</div>",
% @0 r2 v$ E4 i; ? - $out, PREG_SET_ORDER);+ I9 Z5 d, v+ v, g
- print $out[0][0].", ".$out[0][1]."\n";
T$ O: o% L2 Z F! O8 G9 ` - print $out[1][0].", ".$out[1][1]."\n";' W3 I7 g% C7 z& u: O& h
- ?>
銴鋆賭誨蝣
- h6 H7 l! e* |; P6 l. e* ^砌撠颲綽 - <b>example: </b>, example:
+ g* c, A( a7 c& Q+ s% m, Q - <div align=left>this is a test</div>, this is a test
@9 f! \( o& f# N1 b3 D: n
銴鋆賭誨蝣
5 {& R& [5 U3 a5 ^. D4 Y砌銝哨$matches[0] 舐洵銝蝏寥蝏嚗$matches[0][0] 怠寥港葵璅∪穿$matches[0][1] 怠寥蝚砌銝芸璅∪穿隞交迨蝐餅具瘀$matches[1] 舐洵鈭蝏寥蝏嚗蝑蝑 PREG_OFFSET_CAPTURE憒霈曉祆霈堆撖寞銝芸箇啁寥蝏銋嗉園撅摮蝚虫葡蝘駁瘜冽餈孵鈭餈啁潘雿踹嗡葉瘥銝芸銋臭銝芣啁嚗嗡葉蝚砌憿嫣蛹寥摮蝚虫葡嚗蝚砌憿嫣蛹嗅 subject 銝剔蝘駁祆霈啗 PHP 4.3.0 韏瑕舐具 6 Z- V4 i+ }9 F3 s) G3 ~, t
J& X B; J7 D: X. l! f
憒瘝⊥蝏箸霈堆摰銝 PREG_PATTERN_ORDER 餈港葵璅∪寥甈⊥堆航賭蛹塚嚗憒粹餈 FALSE
9 {5 A% f: b. f靘摮 1. 隞砌葉敺菔瑞 - <?php7 F) `/ H: V. U& ?& k1 i
- preg_match_all ("/\(? (\d{3})? \)? (?(1) [\-\s] ) \d{3}-\d{4}/x",# A! n% Q8 G& J0 r8 ]$ Y& D
- "Call 555-1212 or 1-800-555-1212 FREE", $phones);+ `! \5 s2 q1 M9 ~
- ?>
銴鋆賭誨蝣靘摮 2. 蝝W寥 HTML 霈堆greedy嚗 - <?php0 F2 |+ L% g- i; C- O) i3 e' X
- // \\2 臭銝芷撘函靘摮嚗嗅 PCRE 銝剔思) `6 ^8 Q7 ]: ?: ?$ |6 a2 z
- // 敹憿餃寥甇銵刻噢撘祈澈銝剔洵鈭蝏砍瑕摰對砌銝
* `$ q, z Q$ h+ O# e2 u - // 撠望 ([\w]+)銝箏蝚虫葡典撘瑚葉嚗隞仿閬* [2 L" a4 z1 ^* {( r% K
- // 憭銝銝芸蝥踴3 ~" u o( w* B# y' i8 t
- $html = "<b>bold text</b><a href=howdy.html>click me</a>";/ y5 w( R$ W7 D1 N4 t$ v
- 9 D% x2 x: `4 b( Z- E
- preg_match_all ("/(<([\w]+)[^>]*>)(.*)(<\/\\2>)/", $html, $matches);: U% {4 O& L( M, h# I- t* m5 A
* [; y. j v& W" _8 X- for ($i=0; $i< count($matches[0]); $i++) {/ l9 x D8 T/ r. w
- echo "matched: ".$matches[0][$i]."\n";
% |8 F* {4 R) i$ t - echo "part 1: ".$matches[1][$i]."\n";
) }% b) N" L% f; T- L; N - echo "part 2: ".$matches[3][$i]."\n";
( c1 J6 T% ?2 [ - echo "part 3: ".$matches[4][$i]."\n\n";8 \' E2 s0 }, S0 y* ?2 v
- }
f8 R7 ]7 z( c2 ^ - ?>
銴鋆賭誨蝣砌撠颲綽 - matched: <b>bold text</b>% ?5 j h2 D+ x1 `. c* O! z
- part 1: <b>, Y0 Y4 p3 Z) U$ I0 q
- part 2: bold text0 B5 X' g6 i/ t% x
- part 3: </b>, P$ j* p2 D2 R- m3 U
- 1 x( H6 R0 K! K" F( o6 A& f
- matched: <a href=howdy.html>click me</a>
, ?5 q1 b$ g1 ]1 D - part 1: <a href=howdy.html>
4 M2 H, K, r! o' G2 t - part 2: click me
" i; J4 ?) Q5 w - part 3: </a>
銴鋆賭誨蝣 & R/ z9 a5 K3 c; F* S
! t( A3 X- R) f+ b% s |
|