|
int preg_match_all ( string pattern, string subject, array matches [, int flags] )
. Q' O- J2 S3 D, {5 k9 y2 D- G3 t. n% P. S. q
subject 銝剜蝝Xpattern 蝏箇甇閬銵函內撘寥摰嫣蒂撠蝏隞 flags 摰摨曉 matches 銝准 蝝W啁洵銝銝芸寥銋嚗乩亦蝝W銝銝寥怠偏憪 flags 臭誑臭璅閮蝯嚗瘜冽 PREG_PATTERN_ORDER PREG_SET_ORDER 韏瑚冽瓷蝢抬嚗 $ g6 t% A2 B) `- S* j1 a1 u! u) Y
PREG_PATTERN_ORDER撠蝯摨雿 $matches[0] 箏券冽芋撘寥摮銝莎$matches[1]箇洵銝砍瑚葉摮璅∪寥摮銝莎隞交迨憿具 - <?php
. g, F4 p/ z: h2 u* K$ ` - preg_match_all ("|<[^>]+>(.*)</[^>]+>|U",) _2 Q7 B$ J+ ^ `: T
- "<b>example: </b><div align=left>this is a test</div>",
0 \4 c0 ]' t: i' k - $out, PREG_PATTERN_ORDER);
+ Z; d7 Q2 @2 X# K: _ - print $out[0][0].", ".$out[0][1]."\n";7 N4 l* y+ F0 A3 b# O
- print $out[1][0].", ".$out[1][1]."\n";
. o6 ]# `) e7 _ - ?>
銴鋆賭誨蝣
9 v: c( \/ Q3 ^3 r砌撠颲綽 - <b>example: </b>, <div align=left>this is a test</div>, g2 S5 N9 r8 Q6 V ^
- example: , this is a test
銴鋆賭誨蝣 0 }9 t V& J( H0 P9 u
甇歹$out[0] 怠寥港葵璅∪摮銝莎$out[1] 思撠HTML 璅閮銋摮銝脯 $ |* C% w+ @0 K( Q% f' z
/ a8 @5 t; j9 h4 Q
# W+ Y0 [8 `4 ?% t$ s! `. S5 C
PREG_SET_ORDER撖寧摨雿 $matches[0] 銝箇洵銝蝏寥憿寧啁嚗$matches[1] 銝箇洵鈭蝏寥憿寧啁嚗隞交迨蝐餅具 - <?php8 p5 i2 B+ g* [: [% z V: G, Q
- preg_match_all ("|<[^>]+>(.*)</[^>]+>|U",8 S( k8 S! s5 d2 n, }
- "<b>example: </b><div align=left>this is a test</div>",
5 {9 y6 u+ b) r$ ~ - $out, PREG_SET_ORDER);
& C# f* }+ l0 F2 H3 h/ I - print $out[0][0].", ".$out[0][1]."\n";* ~; U, B0 F. i+ Q2 r8 e
- print $out[1][0].", ".$out[1][1]."\n";
/ z! }& j) D" v. y6 i - ?>
銴鋆賭誨蝣
6 u9 K) k9 [7 t2 i& B+ @1 I$ F砌撠颲綽 - <b>example: </b>, example:% G7 Z. \: u- F7 w1 j* e; j
- <div align=left>this is a test</div>, this is a test
) k( P; i! p' I; k5 l
銴鋆賭誨蝣 7 b* Q6 @6 E }, A {
砌銝哨$matches[0] 舐洵銝蝏寥蝏嚗$matches[0][0] 怠寥港葵璅∪穿$matches[0][1] 怠寥蝚砌銝芸璅∪穿隞交迨蝐餅具瘀$matches[1] 舐洵鈭蝏寥蝏嚗蝑蝑 PREG_OFFSET_CAPTURE憒霈曉祆霈堆撖寞銝芸箇啁寥蝏銋嗉園撅摮蝚虫葡蝘駁瘜冽餈孵鈭餈啁潘雿踹嗡葉瘥銝芸銋臭銝芣啁嚗嗡葉蝚砌憿嫣蛹寥摮蝚虫葡嚗蝚砌憿嫣蛹嗅 subject 銝剔蝘駁祆霈啗 PHP 4.3.0 韏瑕舐具
& P5 }/ D. S% A+ v4 v X. y2 Q3 L) [# h9 T c# s O
憒瘝⊥蝏箸霈堆摰銝 PREG_PATTERN_ORDER 餈港葵璅∪寥甈⊥堆航賭蛹塚嚗憒粹餈 FALSE ) G4 Y, m- s) C1 R
靘摮 1. 隞砌葉敺菔瑞 - <?php0 p* \- w9 g" D6 F
- preg_match_all ("/\(? (\d{3})? \)? (?(1) [\-\s] ) \d{3}-\d{4}/x",* ~. u I0 u# @: K v
- "Call 555-1212 or 1-800-555-1212 FREE", $phones);1 e9 Z( m: L2 W- s
- ?>
銴鋆賭誨蝣靘摮 2. 蝝W寥 HTML 霈堆greedy嚗 - <?php
( O* A4 u$ m, c5 x - // \\2 臭銝芷撘函靘摮嚗嗅 PCRE 銝剔思2 P. \; L$ h/ R5 H) }# a% z
- // 敹憿餃寥甇銵刻噢撘祈澈銝剔洵鈭蝏砍瑕摰對砌銝
: |0 i8 ]6 n2 Z. d - // 撠望 ([\w]+)銝箏蝚虫葡典撘瑚葉嚗隞仿閬
p* U7 l9 M, R$ e$ x/ {' k4 e. p - // 憭銝銝芸蝥踴! `- h6 J5 |9 A- I4 b `2 l
- $html = "<b>bold text</b><a href=howdy.html>click me</a>";* d# ^7 l6 h r; q+ }' N u. b& R4 I
- 5 l) d. x- e) y1 h
- preg_match_all ("/(<([\w]+)[^>]*>)(.*)(<\/\\2>)/", $html, $matches);
3 \3 u) E. n6 K9 I. M/ V - : u5 q4 e. r P( Z
- for ($i=0; $i< count($matches[0]); $i++) {! y% M0 _, ]; P8 ?
- echo "matched: ".$matches[0][$i]."\n";
( Z: @ l8 b; O6 q5 q! H7 ~ - echo "part 1: ".$matches[1][$i]."\n";
" p& O- \! j3 S3 r - echo "part 2: ".$matches[3][$i]."\n";
4 w4 Z* A4 e. V5 d2 A; ?+ G - echo "part 3: ".$matches[4][$i]."\n\n";8 A8 q% H4 C4 E- Q. b" a1 w
- }( @) c/ W7 J$ X' S' _4 a: d6 a
- ?>
銴鋆賭誨蝣砌撠颲綽 - matched: <b>bold text</b>- R3 O3 l- v6 Y3 m2 p& b! \
- part 1: <b>" \- U; h* G) k9 z& ]
- part 2: bold text
: v2 H$ ` W$ m- Y$ `2 k - part 3: </b>
* d0 O& _8 i" [' i1 i6 j2 _2 q
' @, Z# _0 j$ Y$ w( \- matched: <a href=howdy.html>click me</a>
7 F! K( z0 V% d- b% l - part 1: <a href=howdy.html>- ~4 T$ v; j) k1 N5 x- t8 T
- part 2: click me
. n+ S' C0 y2 \7 {5 \0 m - part 3: </a>
銴鋆賭誨蝣
4 K+ k2 O, @* o/ I
& F$ e& |* X0 C: W* X0 G: b. o4 t |
|