int preg_match_all ( string pattern, string subject, array matches [, int flags] )
0 g0 S: d# l' s; g# {8 ]3 o9 Q
5 g6 D5 T4 p) s3 N- z! i; [ subject 銝剜蝝Xpattern 蝏箇甇閬銵函內撘寥摰嫣蒂撠蝏隞 flags 摰摨曉 matches 銝准 蝝W啁洵銝銝芸寥銋嚗乩亦蝝W銝銝寥怠偏憪 flags 臭誑臭璅閮蝯嚗瘜冽 PREG_PATTERN_ORDER PREG_SET_ORDER 韏瑚冽瓷蝢抬嚗
0 ~2 R' T1 p% ^' X4 I2 L9 W" R: T5 {PREG_PATTERN_ORDER撠蝯摨雿 $matches[0] 箏券冽芋撘寥摮銝莎$matches[1]箇洵銝砍瑚葉摮璅∪寥摮銝莎隞交迨憿具 - <?php @( C6 r5 X& ]( n( a
- preg_match_all ("|<[^>]+>(.*)</[^>]+>|U",
2 P) P4 _1 h. b$ Z( P - "<b>example: </b><div align=left>this is a test</div>",: K, ?* D$ c/ s1 ?
- $out, PREG_PATTERN_ORDER);8 ]' l) d3 ~' _9 @/ q: u% o$ T4 r
- print $out[0][0].", ".$out[0][1]."\n";8 F; m& T7 W3 m& T+ d' q
- print $out[1][0].", ".$out[1][1]."\n";
, d7 g. l3 X' y; P - ?>
銴鋆賭誨蝣
$ l7 p# \$ Z/ ~* `+ `7 I' Q砌撠颲綽 - <b>example: </b>, <div align=left>this is a test</div>7 K" f1 g% Q! j* v% [9 h8 m
- example: , this is a test
銴鋆賭誨蝣 \5 u& o5 q% n5 g% s
甇歹$out[0] 怠寥港葵璅∪摮銝莎$out[1] 思撠HTML 璅閮銋摮銝脯 ' m7 r! }" E% o& M3 p; K' M
$ \: X4 P$ t7 t8 L4 g( d
# `3 `& F8 l& E. [) z
PREG_SET_ORDER撖寧摨雿 $matches[0] 銝箇洵銝蝏寥憿寧啁嚗$matches[1] 銝箇洵鈭蝏寥憿寧啁嚗隞交迨蝐餅具 - <?php8 q6 c( T, f7 b0 R
- preg_match_all ("|<[^>]+>(.*)</[^>]+>|U",
4 C# c G$ t! o, ~ - "<b>example: </b><div align=left>this is a test</div>",0 t+ I f y8 K2 T) @3 \$ U4 k
- $out, PREG_SET_ORDER);% g0 c& a$ H9 F R, n% m5 R! u
- print $out[0][0].", ".$out[0][1]."\n";
$ y, o. ~& _% k; k - print $out[1][0].", ".$out[1][1]."\n";' k* D+ T- S8 {7 z- s; t9 p
- ?>
銴鋆賭誨蝣 : q& A& _2 x, x7 w- T9 K, l% _
砌撠颲綽 - <b>example: </b>, example:
' f9 S3 n; i5 h - <div align=left>this is a test</div>, this is a test, l# U: K3 p1 y8 A8 i. b- e
銴鋆賭誨蝣
+ `/ R4 a$ p& r$ x) a* C$ ?* }/ o( J砌銝哨$matches[0] 舐洵銝蝏寥蝏嚗$matches[0][0] 怠寥港葵璅∪穿$matches[0][1] 怠寥蝚砌銝芸璅∪穿隞交迨蝐餅具瘀$matches[1] 舐洵鈭蝏寥蝏嚗蝑蝑 PREG_OFFSET_CAPTURE憒霈曉祆霈堆撖寞銝芸箇啁寥蝏銋嗉園撅摮蝚虫葡蝘駁瘜冽餈孵鈭餈啁潘雿踹嗡葉瘥銝芸銋臭銝芣啁嚗嗡葉蝚砌憿嫣蛹寥摮蝚虫葡嚗蝚砌憿嫣蛹嗅 subject 銝剔蝘駁祆霈啗 PHP 4.3.0 韏瑕舐具
4 L' k {7 \" N2 O L' u+ _ Z6 m; u$ R' ~
憒瘝⊥蝏箸霈堆摰銝 PREG_PATTERN_ORDER 餈港葵璅∪寥甈⊥堆航賭蛹塚嚗憒粹餈 FALSE 6 C6 |/ _7 l4 W% @3 }, D. ?) }
靘摮 1. 隞砌葉敺菔瑞 - <?php. _" N% y n" G9 l
- preg_match_all ("/\(? (\d{3})? \)? (?(1) [\-\s] ) \d{3}-\d{4}/x",: } c/ K5 Z3 \4 C" @) X: n. d
- "Call 555-1212 or 1-800-555-1212 FREE", $phones);
% b/ S# t1 H) G$ w) u( C% M8 M - ?>
銴鋆賭誨蝣靘摮 2. 蝝W寥 HTML 霈堆greedy嚗 - <?php0 K3 }1 m$ u6 k. {% G
- // \\2 臭銝芷撘函靘摮嚗嗅 PCRE 銝剔思- x" k3 G9 j$ A; \ }! O( ~
- // 敹憿餃寥甇銵刻噢撘祈澈銝剔洵鈭蝏砍瑕摰對砌銝/ t0 H9 [1 l- S/ C. _ O, Q
- // 撠望 ([\w]+)銝箏蝚虫葡典撘瑚葉嚗隞仿閬
. H" V' ^% Z: |, ^# y* ^- ^ - // 憭銝銝芸蝥踴
3 L7 a2 c3 G, h; p3 E+ a4 d1 V9 W - $html = "<b>bold text</b><a href=howdy.html>click me</a>";+ S/ o/ e; A, |3 b9 t, ]3 n# f
- # r Z, Y3 o8 z+ ~$ X1 b4 W3 A5 l
- preg_match_all ("/(<([\w]+)[^>]*>)(.*)(<\/\\2>)/", $html, $matches);
6 c. o X/ l: I
4 I7 R8 ?" O8 d! Z- for ($i=0; $i< count($matches[0]); $i++) {
# E* G( ]0 L" R0 n, a9 J- L - echo "matched: ".$matches[0][$i]."\n";2 F' c. P$ Z9 D, s" x
- echo "part 1: ".$matches[1][$i]."\n";9 V' }9 E6 l! e6 ^; E0 B" Y
- echo "part 2: ".$matches[3][$i]."\n";7 L& v( Y5 _; ~
- echo "part 3: ".$matches[4][$i]."\n\n";6 J! @, j' U+ q8 [
- }* N8 z/ F6 u, e* G. Q* _4 B
- ?>
銴鋆賭誨蝣砌撠颲綽 - matched: <b>bold text</b>+ k8 _) D9 @5 ~: J1 m1 {
- part 1: <b>
( ?- L3 Q& D# ]. p. O: b! d% r0 f - part 2: bold text r/ a3 r* ^ o' Q9 `/ l. X
- part 3: </b>: a0 J3 `5 Z! L$ T7 D0 u% C! q2 E
5 E X+ s% |: e) I- matched: <a href=howdy.html>click me</a>9 Z! h- e0 u' v7 S+ I3 h
- part 1: <a href=howdy.html>
" M6 J! S' h: U: z1 J2 Y | - part 2: click me
6 H" X0 j3 {: Y" s8 W - part 3: </a>
銴鋆賭誨蝣 # L$ t, y1 q8 Q3 _# t( h
- U- n# N) s* o: g' | |
|