int preg_match_all ( string pattern, string subject, array matches [, int flags] )' i, R7 S. y( L7 N, [
% R: z+ g+ q- z6 | subject 銝剜蝝Xpattern 蝏箇甇閬銵函內撘寥摰嫣蒂撠蝏隞 flags 摰摨曉 matches 銝准 蝝W啁洵銝銝芸寥銋嚗乩亦蝝W銝銝寥怠偏憪 flags 臭誑臭璅閮蝯嚗瘜冽 PREG_PATTERN_ORDER PREG_SET_ORDER 韏瑚冽瓷蝢抬嚗
5 G/ ~' v! r; D) Y; {9 P) Y+ `PREG_PATTERN_ORDER撠蝯摨雿 $matches[0] 箏券冽芋撘寥摮銝莎$matches[1]箇洵銝砍瑚葉摮璅∪寥摮銝莎隞交迨憿具 - <?php: o4 f( y' r7 S) J) L* C8 a4 b: s8 A
- preg_match_all ("|<[^>]+>(.*)</[^>]+>|U",0 ]5 c$ K7 d1 L/ s% G% e
- "<b>example: </b><div align=left>this is a test</div>",. m2 ^# d @. U! R
- $out, PREG_PATTERN_ORDER);
$ B* Y' a) q! b( e" O - print $out[0][0].", ".$out[0][1]."\n";/ ?) x8 j% j2 v% X% E' m$ N
- print $out[1][0].", ".$out[1][1]."\n";
P' K! c4 j' H - ?>
銴鋆賭誨蝣 ; W$ {: Y& d: x( _! }0 x
砌撠颲綽 - <b>example: </b>, <div align=left>this is a test</div>3 c2 |3 ]6 g/ W1 }: U$ l
- example: , this is a test
銴鋆賭誨蝣 2 l y/ P) A4 Z! x) U
甇歹$out[0] 怠寥港葵璅∪摮銝莎$out[1] 思撠HTML 璅閮銋摮銝脯 4 B( z: P/ n8 E( X( k' o5 b
9 M9 R- k \9 P8 |, O3 W
, I. o7 o% P" Y# U/ |, X7 J! S6 O4 NPREG_SET_ORDER撖寧摨雿 $matches[0] 銝箇洵銝蝏寥憿寧啁嚗$matches[1] 銝箇洵鈭蝏寥憿寧啁嚗隞交迨蝐餅具 - <?php2 S3 X) o/ G+ K5 d
- preg_match_all ("|<[^>]+>(.*)</[^>]+>|U",9 e5 M" o- M' h" o1 X6 Y8 _+ ~
- "<b>example: </b><div align=left>this is a test</div>",
1 E, x; a. ~( `# s r - $out, PREG_SET_ORDER);6 B7 G: T! b" @0 M9 |3 m# u
- print $out[0][0].", ".$out[0][1]."\n";
7 x) b+ X+ [9 g1 v - print $out[1][0].", ".$out[1][1]."\n";
1 _" E* _6 u, M2 `, Z' j - ?>
銴鋆賭誨蝣
9 r2 R* A; a+ x I$ D砌撠颲綽 - <b>example: </b>, example:
8 {& V% a' J) v! o2 F6 t) d; z - <div align=left>this is a test</div>, this is a test
5 @6 W$ a2 |0 x/ b( X' S, x" ?; n8 M
銴鋆賭誨蝣
0 @! Y0 }4 ?" x8 \9 F0 c6 F砌銝哨$matches[0] 舐洵銝蝏寥蝏嚗$matches[0][0] 怠寥港葵璅∪穿$matches[0][1] 怠寥蝚砌銝芸璅∪穿隞交迨蝐餅具瘀$matches[1] 舐洵鈭蝏寥蝏嚗蝑蝑 PREG_OFFSET_CAPTURE憒霈曉祆霈堆撖寞銝芸箇啁寥蝏銋嗉園撅摮蝚虫葡蝘駁瘜冽餈孵鈭餈啁潘雿踹嗡葉瘥銝芸銋臭銝芣啁嚗嗡葉蝚砌憿嫣蛹寥摮蝚虫葡嚗蝚砌憿嫣蛹嗅 subject 銝剔蝘駁祆霈啗 PHP 4.3.0 韏瑕舐具
/ t$ p( ~# [0 V, Y& ^7 S6 s
* |; |% v0 j* g4 v/ q, h( ~憒瘝⊥蝏箸霈堆摰銝 PREG_PATTERN_ORDER 餈港葵璅∪寥甈⊥堆航賭蛹塚嚗憒粹餈 FALSE & e, l2 {* O) l7 P0 r) y; H: A( T- m0 k. k
靘摮 1. 隞砌葉敺菔瑞 - <?php6 }2 n4 o) C1 @. M8 Y
- preg_match_all ("/\(? (\d{3})? \)? (?(1) [\-\s] ) \d{3}-\d{4}/x",
V" k$ D3 B ~ - "Call 555-1212 or 1-800-555-1212 FREE", $phones);6 q( K$ j+ z; A5 v* j
- ?>
銴鋆賭誨蝣靘摮 2. 蝝W寥 HTML 霈堆greedy嚗 - <?php
3 g, K; m+ a( R- |# K+ y - // \\2 臭銝芷撘函靘摮嚗嗅 PCRE 銝剔思
9 u+ P- U- L- r2 ~1 N# M- G( d - // 敹憿餃寥甇銵刻噢撘祈澈銝剔洵鈭蝏砍瑕摰對砌銝
1 b3 r5 q5 s e - // 撠望 ([\w]+)銝箏蝚虫葡典撘瑚葉嚗隞仿閬9 D6 f8 }3 p7 N" n
- // 憭銝銝芸蝥踴; F. i, Y7 |5 v) ?1 o2 _1 } g0 P
- $html = "<b>bold text</b><a href=howdy.html>click me</a>";
0 D: v3 ]- B0 b: q E+ g - & k2 s. O8 J5 f& q2 U/ B4 V& a
- preg_match_all ("/(<([\w]+)[^>]*>)(.*)(<\/\\2>)/", $html, $matches);
2 F8 T; ]: y' L+ \) s& n - 6 r' C* a- ~3 e3 C+ c: \( w. F
- for ($i=0; $i< count($matches[0]); $i++) {; N0 Z+ [- `" H, G# \# V1 G2 V& O" h
- echo "matched: ".$matches[0][$i]."\n";
% V( q5 E1 Z+ {* u& E - echo "part 1: ".$matches[1][$i]."\n";0 q0 N0 F6 d6 P: `; O
- echo "part 2: ".$matches[3][$i]."\n";/ T) x5 E! S3 I2 p9 p
- echo "part 3: ".$matches[4][$i]."\n\n";7 M1 |* A& @: a" ~' Y2 o' D
- }
6 }& \% ^' v& x. r2 Z - ?>
銴鋆賭誨蝣砌撠颲綽 - matched: <b>bold text</b>
7 Z* i. n9 C6 H2 z) e* m( Y - part 1: <b>
: a0 [3 r" ^9 b5 |6 J3 m - part 2: bold text
1 ^* n0 A% |1 f* b. C - part 3: </b>0 d& f' j- f/ c/ `9 h | _4 c
- % f7 c. g! W: g) H
- matched: <a href=howdy.html>click me</a>
" V- M" Y4 W) G+ K* E - part 1: <a href=howdy.html>) T5 i( {2 h1 G7 M- s2 B/ Y$ c
- part 2: click me) @# \! K5 r7 P# G) x* }
- part 3: </a>
銴鋆賭誨蝣
/ S$ Z! F2 q v
c$ u. ]( ]; q3 v' p; M' e- H' | |
|