int preg_match_all ( string pattern, string subject, array matches [, int flags] )
/ O l% u% |- e1 g/ T9 K) ~$ {) H6 F# A; b: A, T. C1 R, _. t
subject 銝剜蝝Xpattern 蝏箇甇閬銵函內撘寥摰嫣蒂撠蝏隞 flags 摰摨曉 matches 銝准 蝝W啁洵銝銝芸寥銋嚗乩亦蝝W銝銝寥怠偏憪 flags 臭誑臭璅閮蝯嚗瘜冽 PREG_PATTERN_ORDER PREG_SET_ORDER 韏瑚冽瓷蝢抬嚗 : j# f4 o X, v9 _/ ?5 w0 z j
PREG_PATTERN_ORDER撠蝯摨雿 $matches[0] 箏券冽芋撘寥摮銝莎$matches[1]箇洵銝砍瑚葉摮璅∪寥摮銝莎隞交迨憿具 - <?php
- [9 p7 T' R. x4 I0 ]1 G - preg_match_all ("|<[^>]+>(.*)</[^>]+>|U",1 M. T, X# C: h; C) L
- "<b>example: </b><div align=left>this is a test</div>",
4 Y/ d4 I: N- W- @! R - $out, PREG_PATTERN_ORDER);
5 \+ }0 v6 l+ u - print $out[0][0].", ".$out[0][1]."\n";% r! U! T z6 m/ W5 G: K
- print $out[1][0].", ".$out[1][1]."\n";
/ H4 M9 H, Y7 n. ]5 R1 Z0 Z - ?>
銴鋆賭誨蝣 2 A! e! a: i; ]7 n. i2 Z2 g/ h
砌撠颲綽 - <b>example: </b>, <div align=left>this is a test</div>" S1 S8 [ f" D
- example: , this is a test
銴鋆賭誨蝣
; ^" ~! o' S( g6 E3 o q甇歹$out[0] 怠寥港葵璅∪摮銝莎$out[1] 思撠HTML 璅閮銋摮銝脯
! L. O9 p6 E. E, R* e% F0 {9 X" {, s1 h6 q8 V8 k9 ]
. }6 Q. z% ]7 a+ o$ m# IPREG_SET_ORDER撖寧摨雿 $matches[0] 銝箇洵銝蝏寥憿寧啁嚗$matches[1] 銝箇洵鈭蝏寥憿寧啁嚗隞交迨蝐餅具 - <?php
% B* K h! A5 r - preg_match_all ("|<[^>]+>(.*)</[^>]+>|U",
' `, R1 `& A) T - "<b>example: </b><div align=left>this is a test</div>",- h( W. R( D' T0 U
- $out, PREG_SET_ORDER);
- g9 H( J5 p0 t6 h3 }$ ?1 M - print $out[0][0].", ".$out[0][1]."\n";
4 G$ |, y" y* }1 s% ~2 q2 ] - print $out[1][0].", ".$out[1][1]."\n";
6 ?% R {8 [& {1 T - ?>
銴鋆賭誨蝣 + w6 S2 _5 w9 u s1 J
砌撠颲綽 - <b>example: </b>, example:4 ?) e \2 F* l9 m* l l/ @
- <div align=left>this is a test</div>, this is a test
' I5 l: j+ l! G
銴鋆賭誨蝣
6 O' x% o* m& Q+ F7 m& W4 m砌銝哨$matches[0] 舐洵銝蝏寥蝏嚗$matches[0][0] 怠寥港葵璅∪穿$matches[0][1] 怠寥蝚砌銝芸璅∪穿隞交迨蝐餅具瘀$matches[1] 舐洵鈭蝏寥蝏嚗蝑蝑 PREG_OFFSET_CAPTURE憒霈曉祆霈堆撖寞銝芸箇啁寥蝏銋嗉園撅摮蝚虫葡蝘駁瘜冽餈孵鈭餈啁潘雿踹嗡葉瘥銝芸銋臭銝芣啁嚗嗡葉蝚砌憿嫣蛹寥摮蝚虫葡嚗蝚砌憿嫣蛹嗅 subject 銝剔蝘駁祆霈啗 PHP 4.3.0 韏瑕舐具
( o/ U; T( W+ N0 I Q# O: P7 L1 N) v$ O2 T+ |+ p
憒瘝⊥蝏箸霈堆摰銝 PREG_PATTERN_ORDER 餈港葵璅∪寥甈⊥堆航賭蛹塚嚗憒粹餈 FALSE
+ [- d2 g- j v/ w" L' F靘摮 1. 隞砌葉敺菔瑞 - <?php3 w' ~! L. t( D) q
- preg_match_all ("/\(? (\d{3})? \)? (?(1) [\-\s] ) \d{3}-\d{4}/x",
# c( q: n& |+ s8 u; W8 X8 { - "Call 555-1212 or 1-800-555-1212 FREE", $phones);( S1 `( _. ^3 @0 H
- ?>
銴鋆賭誨蝣靘摮 2. 蝝W寥 HTML 霈堆greedy嚗 - <?php" i3 U7 E3 P$ i# n* v
- // \\2 臭銝芷撘函靘摮嚗嗅 PCRE 銝剔思
7 K) q9 n+ l0 z) u$ d* T - // 敹憿餃寥甇銵刻噢撘祈澈銝剔洵鈭蝏砍瑕摰對砌銝. q7 `# ?0 {; g5 V/ `
- // 撠望 ([\w]+)銝箏蝚虫葡典撘瑚葉嚗隞仿閬7 s5 G V; [$ P6 t: w6 c# `: i
- // 憭銝銝芸蝥踴! t7 x5 C" L9 H' F" m& N( r: F
- $html = "<b>bold text</b><a href=howdy.html>click me</a>";
' y, i& g @$ W) @1 W8 ~4 G - 1 c8 l! [ w7 _6 g8 L8 B
- preg_match_all ("/(<([\w]+)[^>]*>)(.*)(<\/\\2>)/", $html, $matches);1 `, h) A3 o( f \- z9 U( N
- 1 T7 h* A8 `7 D, H( _0 ]
- for ($i=0; $i< count($matches[0]); $i++) {% G' q: g0 h9 G
- echo "matched: ".$matches[0][$i]."\n";
( v4 O. q/ l5 U6 i! |: t - echo "part 1: ".$matches[1][$i]."\n";
, B5 j' R+ p& O3 ] - echo "part 2: ".$matches[3][$i]."\n";
2 j* s8 m5 y, Y% _* @ - echo "part 3: ".$matches[4][$i]."\n\n";+ y% W& A: W8 \+ L' b* i% s& K1 j& [
- }
2 M7 L( }- v+ |% Y - ?>
銴鋆賭誨蝣砌撠颲綽 - matched: <b>bold text</b>
* M+ y4 X% u2 k* @- @; C3 A" X% F - part 1: <b>2 x+ F$ ?: w- u. O7 L: X' Z
- part 2: bold text* X; `0 z( W( v( o
- part 3: </b>4 X o3 ?( `" q) n* ` b q9 `+ f
- / E7 n- H" b5 F* U4 L, N+ N5 S
- matched: <a href=howdy.html>click me</a>. [8 X/ x+ I' k8 j
- part 1: <a href=howdy.html>1 L$ Q$ e0 A3 T- J; j
- part 2: click me$ X% M; ~; w5 ]- D3 V2 g
- part 3: </a>
銴鋆賭誨蝣
& S. `/ n% Y- q; f, _
. \) d6 g8 K. P4 F' K |
|