int preg_match_all ( string pattern, string subject, array matches [, int flags] )+ \9 ^' `3 v" t2 W
# M! }7 A+ z/ W8 U0 t% G6 k4 O' D subject 銝剜蝝Xpattern 蝏箇甇閬銵函內撘寥摰嫣蒂撠蝏隞 flags 摰摨曉 matches 銝准 蝝W啁洵銝銝芸寥銋嚗乩亦蝝W銝銝寥怠偏憪 flags 臭誑臭璅閮蝯嚗瘜冽 PREG_PATTERN_ORDER PREG_SET_ORDER 韏瑚冽瓷蝢抬嚗 " N- c0 O) n+ M
PREG_PATTERN_ORDER撠蝯摨雿 $matches[0] 箏券冽芋撘寥摮銝莎$matches[1]箇洵銝砍瑚葉摮璅∪寥摮銝莎隞交迨憿具 - <?php, w h5 k# X F s; F7 b
- preg_match_all ("|<[^>]+>(.*)</[^>]+>|U",5 ~9 p2 i/ H3 `$ l) ]) R+ l, s) r
- "<b>example: </b><div align=left>this is a test</div>",. C. Q8 m3 L5 h" u( c" A# N
- $out, PREG_PATTERN_ORDER);- y8 X7 E; ~/ D9 p( S v
- print $out[0][0].", ".$out[0][1]."\n";
, \+ W7 S p2 a' C \ - print $out[1][0].", ".$out[1][1]."\n";
" h! y1 y* r8 p) B! V& M% H! W - ?>
銴鋆賭誨蝣
9 S V9 c8 J$ C/ {' r, [9 v) \砌撠颲綽 - <b>example: </b>, <div align=left>this is a test</div>
7 D/ ^" {% `4 g - example: , this is a test
銴鋆賭誨蝣 9 I6 x0 G- @: f \/ ~/ K0 I
甇歹$out[0] 怠寥港葵璅∪摮銝莎$out[1] 思撠HTML 璅閮銋摮銝脯 " F# ^9 D% f# g0 [
/ g3 k* x) N7 U2 { y, Z( S" S: A$ ^* D, u3 P7 w% }& _0 Y
PREG_SET_ORDER撖寧摨雿 $matches[0] 銝箇洵銝蝏寥憿寧啁嚗$matches[1] 銝箇洵鈭蝏寥憿寧啁嚗隞交迨蝐餅具 - <?php
* ^ Y! j6 B, |' l' {* g" e - preg_match_all ("|<[^>]+>(.*)</[^>]+>|U",9 \( _+ N2 I+ p
- "<b>example: </b><div align=left>this is a test</div>",* m1 f* A* f0 Y
- $out, PREG_SET_ORDER);
6 D. c0 q% a0 Z! e5 M+ q - print $out[0][0].", ".$out[0][1]."\n";
% R$ s% K2 z" F0 V. [7 k - print $out[1][0].", ".$out[1][1]."\n"; a+ ~3 E6 n, Z, {4 R: Y! }
- ?>
銴鋆賭誨蝣 + J5 J, E6 g( H3 g5 K6 X) G) A2 R! K
砌撠颲綽 - <b>example: </b>, example:
6 U4 O: f/ U% ^* R/ U* [, s1 X - <div align=left>this is a test</div>, this is a test5 W' B+ z3 Z, g9 ]! C8 S" W8 k
銴鋆賭誨蝣 6 F) R8 H( o% `% U& [2 P+ S5 S
砌銝哨$matches[0] 舐洵銝蝏寥蝏嚗$matches[0][0] 怠寥港葵璅∪穿$matches[0][1] 怠寥蝚砌銝芸璅∪穿隞交迨蝐餅具瘀$matches[1] 舐洵鈭蝏寥蝏嚗蝑蝑 PREG_OFFSET_CAPTURE憒霈曉祆霈堆撖寞銝芸箇啁寥蝏銋嗉園撅摮蝚虫葡蝘駁瘜冽餈孵鈭餈啁潘雿踹嗡葉瘥銝芸銋臭銝芣啁嚗嗡葉蝚砌憿嫣蛹寥摮蝚虫葡嚗蝚砌憿嫣蛹嗅 subject 銝剔蝘駁祆霈啗 PHP 4.3.0 韏瑕舐具
" |6 r7 [! k J
" q' r ~# i: e0 O6 l" w憒瘝⊥蝏箸霈堆摰銝 PREG_PATTERN_ORDER 餈港葵璅∪寥甈⊥堆航賭蛹塚嚗憒粹餈 FALSE
% c+ [" R, ^0 f8 x! | F \& ~靘摮 1. 隞砌葉敺菔瑞 - <?php; Q3 R' b3 A0 K
- preg_match_all ("/\(? (\d{3})? \)? (?(1) [\-\s] ) \d{3}-\d{4}/x",8 C( C5 L4 C1 W- z' ^' v' d
- "Call 555-1212 or 1-800-555-1212 FREE", $phones);
1 X5 E# ~( ^. }+ t7 p$ Z - ?>
銴鋆賭誨蝣靘摮 2. 蝝W寥 HTML 霈堆greedy嚗 - <?php
; o7 C7 _3 {$ n% L2 {+ j - // \\2 臭銝芷撘函靘摮嚗嗅 PCRE 銝剔思% h9 }. y* T) I
- // 敹憿餃寥甇銵刻噢撘祈澈銝剔洵鈭蝏砍瑕摰對砌銝5 T- v$ B, c- d
- // 撠望 ([\w]+)銝箏蝚虫葡典撘瑚葉嚗隞仿閬
2 k4 S$ a* j. N) b8 Y' ?' \5 j& Q - // 憭銝銝芸蝥踴# j4 Q! S/ Y8 R& X1 M5 E3 @/ [
- $html = "<b>bold text</b><a href=howdy.html>click me</a>";' k7 b( P. J' a( b
- F; ~5 ~. j% M& M; Y: J- preg_match_all ("/(<([\w]+)[^>]*>)(.*)(<\/\\2>)/", $html, $matches);: K% c+ Y( p9 |# V
$ o* @4 U& I' n9 G& b4 Q8 @- for ($i=0; $i< count($matches[0]); $i++) {. b* E& U' M7 {7 M, p( a
- echo "matched: ".$matches[0][$i]."\n";
% D; M9 ~; V1 B+ r2 F - echo "part 1: ".$matches[1][$i]."\n";
) x, ?1 z, C3 `% Q" V& d - echo "part 2: ".$matches[3][$i]."\n";
?5 [7 Y% S7 N& v5 A# K - echo "part 3: ".$matches[4][$i]."\n\n";) S1 z7 V" @; P9 ^
- }, T1 o% S9 H* N
- ?>
銴鋆賭誨蝣砌撠颲綽 - matched: <b>bold text</b>
3 m& }" \% e! ~* \4 K - part 1: <b>2 X0 j( m* m9 L
- part 2: bold text" A9 u1 N5 |! |
- part 3: </b>
- c- d3 W& i" t - & ~. W( h) Z. q: r( y/ i! x: X4 T2 A
- matched: <a href=howdy.html>click me</a>
+ D9 [: X8 O! {% D - part 1: <a href=howdy.html>! T3 X7 m! b3 h8 {9 N
- part 2: click me- Y7 W* R% [' W3 I2 p7 ~. E% x
- part 3: </a>
銴鋆賭誨蝣
7 M b) R2 W! X: D3 {2 Y6 U1 L# p* i, h
|
|