|
int preg_match_all ( string pattern, string subject, array matches [, int flags] )2 Z7 O3 I5 s/ Q" ]0 C% L0 T' ]
2 ~& }5 H2 c: h% v# y6 l% y
subject 銝剜蝝Xpattern 蝏箇甇閬銵函內撘寥摰嫣蒂撠蝏隞 flags 摰摨曉 matches 銝准 蝝W啁洵銝銝芸寥銋嚗乩亦蝝W銝銝寥怠偏憪 flags 臭誑臭璅閮蝯嚗瘜冽 PREG_PATTERN_ORDER PREG_SET_ORDER 韏瑚冽瓷蝢抬嚗 0 }' D& T: j! L& ]' E
PREG_PATTERN_ORDER撠蝯摨雿 $matches[0] 箏券冽芋撘寥摮銝莎$matches[1]箇洵銝砍瑚葉摮璅∪寥摮銝莎隞交迨憿具 - <?php0 H+ ]7 @* f, }2 H5 n
- preg_match_all ("|<[^>]+>(.*)</[^>]+>|U",
( [9 n& U( R! E& [1 ?; ^) c - "<b>example: </b><div align=left>this is a test</div>",
; F. Y4 V% s! L - $out, PREG_PATTERN_ORDER);( _9 C8 e; G0 y( L6 L
- print $out[0][0].", ".$out[0][1]."\n";
9 r) K8 ^. q5 Q( o. r Y6 c - print $out[1][0].", ".$out[1][1]."\n";
* M$ | B, w, g - ?>
銴鋆賭誨蝣
1 a( A* }; A* b1 X7 b- X4 {砌撠颲綽 - <b>example: </b>, <div align=left>this is a test</div>4 R# f/ ]6 o1 e( f
- example: , this is a test
銴鋆賭誨蝣 5 O9 y2 ]% U2 [ ]
甇歹$out[0] 怠寥港葵璅∪摮銝莎$out[1] 思撠HTML 璅閮銋摮銝脯
; p% x! t. q6 W
0 E, |0 v$ j/ k* }
$ U+ [0 R7 C- Z8 m, v, M6 c6 T' I5 DPREG_SET_ORDER撖寧摨雿 $matches[0] 銝箇洵銝蝏寥憿寧啁嚗$matches[1] 銝箇洵鈭蝏寥憿寧啁嚗隞交迨蝐餅具 - <?php
+ e8 N% x, b! Q - preg_match_all ("|<[^>]+>(.*)</[^>]+>|U",
; {1 q; h9 F' j5 G; W, G7 M - "<b>example: </b><div align=left>this is a test</div>",
* B- S- t3 H2 ?3 V0 i9 k) m - $out, PREG_SET_ORDER);- q. {9 W& C8 I2 {1 Z+ `0 I3 \$ |
- print $out[0][0].", ".$out[0][1]."\n";
/ k8 v0 O6 x8 a/ Y9 F( B8 ` ?7 r U - print $out[1][0].", ".$out[1][1]."\n";* T/ [) ~# J; ^. ^; y
- ?>
銴鋆賭誨蝣
# Q' Q) c8 V& F2 m) Y$ \砌撠颲綽 - <b>example: </b>, example:
4 A+ p' a* r/ ?; p - <div align=left>this is a test</div>, this is a test' N4 h+ K' k5 N4 h+ N
銴鋆賭誨蝣
$ P2 Y. s, o: h5 i/ G砌銝哨$matches[0] 舐洵銝蝏寥蝏嚗$matches[0][0] 怠寥港葵璅∪穿$matches[0][1] 怠寥蝚砌銝芸璅∪穿隞交迨蝐餅具瘀$matches[1] 舐洵鈭蝏寥蝏嚗蝑蝑 PREG_OFFSET_CAPTURE憒霈曉祆霈堆撖寞銝芸箇啁寥蝏銋嗉園撅摮蝚虫葡蝘駁瘜冽餈孵鈭餈啁潘雿踹嗡葉瘥銝芸銋臭銝芣啁嚗嗡葉蝚砌憿嫣蛹寥摮蝚虫葡嚗蝚砌憿嫣蛹嗅 subject 銝剔蝘駁祆霈啗 PHP 4.3.0 韏瑕舐具 ; g+ F( e% Z) t' U% y
9 b1 E6 E! c! \! \$ k, h! i憒瘝⊥蝏箸霈堆摰銝 PREG_PATTERN_ORDER 餈港葵璅∪寥甈⊥堆航賭蛹塚嚗憒粹餈 FALSE
% f4 |% q( Z N0 \' Y/ ^; C0 b- t9 A5 q靘摮 1. 隞砌葉敺菔瑞 - <?php) B6 `3 N& n* @/ r) c
- preg_match_all ("/\(? (\d{3})? \)? (?(1) [\-\s] ) \d{3}-\d{4}/x",
' I1 j: k5 o# A - "Call 555-1212 or 1-800-555-1212 FREE", $phones);
3 j. P* g; [ E& y: M4 ^5 q! | - ?>
銴鋆賭誨蝣靘摮 2. 蝝W寥 HTML 霈堆greedy嚗 - <?php
/ p7 x! w; O, a1 h - // \\2 臭銝芷撘函靘摮嚗嗅 PCRE 銝剔思
% Q% f& h) E- U5 ^ - // 敹憿餃寥甇銵刻噢撘祈澈銝剔洵鈭蝏砍瑕摰對砌銝! A* p# q8 v8 ]- h+ b7 ^
- // 撠望 ([\w]+)銝箏蝚虫葡典撘瑚葉嚗隞仿閬' T) e! _$ p g2 h
- // 憭銝銝芸蝥踴/ i2 N; w% H) y
- $html = "<b>bold text</b><a href=howdy.html>click me</a>";( l% {: J7 Z; b- B8 p3 \$ H& P* \
- # i& l4 }, t; P1 F
- preg_match_all ("/(<([\w]+)[^>]*>)(.*)(<\/\\2>)/", $html, $matches);& e+ H$ M/ ~& ? J
- - P1 u5 N0 ~5 ^. R' i: c# q
- for ($i=0; $i< count($matches[0]); $i++) {
& J& G- W# M' \9 r% } - echo "matched: ".$matches[0][$i]."\n";0 m. b1 ?/ D/ [
- echo "part 1: ".$matches[1][$i]."\n";# E; m* X$ l1 u
- echo "part 2: ".$matches[3][$i]."\n";4 N$ N3 j; i. W
- echo "part 3: ".$matches[4][$i]."\n\n";; Y4 }7 c$ l0 y: W; [8 c! j, V' B
- }
6 C" s. ~, ]$ U - ?>
銴鋆賭誨蝣砌撠颲綽 - matched: <b>bold text</b>
3 _# f; y r* d2 b f& j - part 1: <b>5 A1 E% ]' a$ P( E6 E% r% D$ |
- part 2: bold text0 S* w& R0 a0 g# X) x- y
- part 3: </b>
2 L$ g+ X5 P u j$ j% ?2 a
, G) _9 l7 f- D: W- matched: <a href=howdy.html>click me</a>/ [" M# B( @5 H! |
- part 1: <a href=howdy.html>
' d, g% O# e! | - part 2: click me8 d' D# \5 a) |* L- @9 p
- part 3: </a>
銴鋆賭誨蝣 7 u& W% C A S7 c+ M. J3 N
* a- Z) m3 S+ i1 u
|
|