int preg_match_all ( string pattern, string subject, array matches [, int flags] )
5 |4 t' `, J2 H% ?4 S; c" x+ w6 t6 c( s5 D! C% h
subject 銝剜蝝Xpattern 蝏箇甇閬銵函內撘寥摰嫣蒂撠蝏隞 flags 摰摨曉 matches 銝准 蝝W啁洵銝銝芸寥銋嚗乩亦蝝W銝銝寥怠偏憪 flags 臭誑臭璅閮蝯嚗瘜冽 PREG_PATTERN_ORDER PREG_SET_ORDER 韏瑚冽瓷蝢抬嚗
' H: E- ^: N, j, ~4 r8 @( F( gPREG_PATTERN_ORDER撠蝯摨雿 $matches[0] 箏券冽芋撘寥摮銝莎$matches[1]箇洵銝砍瑚葉摮璅∪寥摮銝莎隞交迨憿具 - <?php
! s( r9 ^9 ]- g7 i2 q( Y - preg_match_all ("|<[^>]+>(.*)</[^>]+>|U",1 r. x4 r' t% A$ z7 S
- "<b>example: </b><div align=left>this is a test</div>",0 ^$ W% v) M1 d/ s
- $out, PREG_PATTERN_ORDER);# t! m4 z( i% O
- print $out[0][0].", ".$out[0][1]."\n";0 _5 F6 W# D' t7 S7 D8 U. Y
- print $out[1][0].", ".$out[1][1]."\n";+ g9 t% h' L/ T4 {" N
- ?>
銴鋆賭誨蝣 0 o$ ^( N' ~1 S8 I! W, q% j+ G
砌撠颲綽 - <b>example: </b>, <div align=left>this is a test</div>
5 N3 l: X" H& E2 P - example: , this is a test
銴鋆賭誨蝣
8 q8 Y, B' v" N/ V甇歹$out[0] 怠寥港葵璅∪摮銝莎$out[1] 思撠HTML 璅閮銋摮銝脯
" d5 w' F, j* q4 H# i( l: T8 v; q5 K- C
! ~3 y3 v/ Z& Y& U0 I! z) G! JPREG_SET_ORDER撖寧摨雿 $matches[0] 銝箇洵銝蝏寥憿寧啁嚗$matches[1] 銝箇洵鈭蝏寥憿寧啁嚗隞交迨蝐餅具 - <?php$ [& I" w; e6 _$ b7 O3 [ z
- preg_match_all ("|<[^>]+>(.*)</[^>]+>|U",8 O3 a3 k6 S$ o. p
- "<b>example: </b><div align=left>this is a test</div>",0 R/ S9 v4 `0 W) q5 Y7 W+ P
- $out, PREG_SET_ORDER);; F6 p7 N r& i$ v
- print $out[0][0].", ".$out[0][1]."\n";
- a1 z; i; l& K6 Q! g' l; b - print $out[1][0].", ".$out[1][1]."\n";3 r( N5 n1 z" _- C* G. B
- ?>
銴鋆賭誨蝣 8 R! z/ y+ r8 W! `- H+ p" V
砌撠颲綽 - <b>example: </b>, example:& W1 z. y- q# d) Q2 V
- <div align=left>this is a test</div>, this is a test3 N9 A1 A2 U a/ j) i$ g
銴鋆賭誨蝣
3 D/ X4 ~7 g0 Y+ v砌銝哨$matches[0] 舐洵銝蝏寥蝏嚗$matches[0][0] 怠寥港葵璅∪穿$matches[0][1] 怠寥蝚砌銝芸璅∪穿隞交迨蝐餅具瘀$matches[1] 舐洵鈭蝏寥蝏嚗蝑蝑 PREG_OFFSET_CAPTURE憒霈曉祆霈堆撖寞銝芸箇啁寥蝏銋嗉園撅摮蝚虫葡蝘駁瘜冽餈孵鈭餈啁潘雿踹嗡葉瘥銝芸銋臭銝芣啁嚗嗡葉蝚砌憿嫣蛹寥摮蝚虫葡嚗蝚砌憿嫣蛹嗅 subject 銝剔蝘駁祆霈啗 PHP 4.3.0 韏瑕舐具 : ]6 R% s# G; B+ I
- M5 M. ?3 z6 w/ S
憒瘝⊥蝏箸霈堆摰銝 PREG_PATTERN_ORDER 餈港葵璅∪寥甈⊥堆航賭蛹塚嚗憒粹餈 FALSE 7 m, `" B" Z# H9 T0 a9 Y
靘摮 1. 隞砌葉敺菔瑞 - <?php% E$ O1 w! E# m0 p& t$ ^6 Y: I
- preg_match_all ("/\(? (\d{3})? \)? (?(1) [\-\s] ) \d{3}-\d{4}/x",- \9 V. D; j9 p# I; w
- "Call 555-1212 or 1-800-555-1212 FREE", $phones);
! z% P& \4 q+ P; ]+ z9 V0 v& f, A: Z - ?>
銴鋆賭誨蝣靘摮 2. 蝝W寥 HTML 霈堆greedy嚗 - <?php c& B1 w6 ]5 N) E
- // \\2 臭銝芷撘函靘摮嚗嗅 PCRE 銝剔思
9 A m4 R3 O: G$ z. J3 W V - // 敹憿餃寥甇銵刻噢撘祈澈銝剔洵鈭蝏砍瑕摰對砌銝+ q, `- r5 y6 ?9 z) p
- // 撠望 ([\w]+)銝箏蝚虫葡典撘瑚葉嚗隞仿閬
) N: i! K U6 C8 F* ` - // 憭銝銝芸蝥踴# D% h# J, E9 Q2 O, l' V: D# l+ ]9 r
- $html = "<b>bold text</b><a href=howdy.html>click me</a>";
e7 s3 T6 i6 x0 T - - K0 c4 x0 i; M0 {) ^
- preg_match_all ("/(<([\w]+)[^>]*>)(.*)(<\/\\2>)/", $html, $matches);1 p. ~' Y. X1 c- g
: ^4 F' ?5 F2 }8 D4 d0 W- for ($i=0; $i< count($matches[0]); $i++) {
3 \8 R( e: ]$ _( t - echo "matched: ".$matches[0][$i]."\n";9 A8 t( M, s) J7 ~6 B
- echo "part 1: ".$matches[1][$i]."\n";
7 M1 o, k4 c' Y; s# H' a - echo "part 2: ".$matches[3][$i]."\n";
: J% ~% P k& N) _! m - echo "part 3: ".$matches[4][$i]."\n\n";
. w/ u: n3 q% U. i - }: P- k. L3 i3 {
- ?>
銴鋆賭誨蝣砌撠颲綽 - matched: <b>bold text</b>/ Y6 @- t3 M( V; V7 | J
- part 1: <b>
: g( I' y$ j( @ e - part 2: bold text
# F G# y2 y" g- b; @( b - part 3: </b>
0 b8 Y p( N l( s# t
( G+ t: K2 K* q s- matched: <a href=howdy.html>click me</a>
$ k+ y6 F1 [ Y5 m0 p6 X1 ? - part 1: <a href=howdy.html>
/ d. H- Y* k. r6 Q7 Y - part 2: click me
, M0 g; f5 X' T$ L; D) } - part 3: </a>
銴鋆賭誨蝣 - n O- c$ ~/ D- H* K
2 k) B+ a4 M: }. \# W |
|