|
int preg_match_all ( string pattern, string subject, array matches [, int flags] )
$ g6 C9 W* B7 E
! {; X4 `% y/ H" e4 _) l6 U subject 銝剜蝝Xpattern 蝏箇甇閬銵函內撘寥摰嫣蒂撠蝏隞 flags 摰摨曉 matches 銝准 蝝W啁洵銝銝芸寥銋嚗乩亦蝝W銝銝寥怠偏憪 flags 臭誑臭璅閮蝯嚗瘜冽 PREG_PATTERN_ORDER PREG_SET_ORDER 韏瑚冽瓷蝢抬嚗
8 {% @! i c. U3 S. MPREG_PATTERN_ORDER撠蝯摨雿 $matches[0] 箏券冽芋撘寥摮銝莎$matches[1]箇洵銝砍瑚葉摮璅∪寥摮銝莎隞交迨憿具 - <?php
6 l, a& p/ Z1 G, Z' B( X - preg_match_all ("|<[^>]+>(.*)</[^>]+>|U",
; Z7 H+ t8 m+ V' r& S, R - "<b>example: </b><div align=left>this is a test</div>",
9 r8 p; {- f j# a; x; z5 c" ]/ h - $out, PREG_PATTERN_ORDER);
; l. P9 g, n! n) c% ? - print $out[0][0].", ".$out[0][1]."\n";- N Y- d8 L7 R! I
- print $out[1][0].", ".$out[1][1]."\n";. c1 T1 J1 H* T8 X
- ?>
銴鋆賭誨蝣
3 v+ G d: d8 t0 t* u( u砌撠颲綽 - <b>example: </b>, <div align=left>this is a test</div>
- E( G+ ^) R- F6 l - example: , this is a test
銴鋆賭誨蝣
% ~! i' L4 n. b8 D甇歹$out[0] 怠寥港葵璅∪摮銝莎$out[1] 思撠HTML 璅閮銋摮銝脯
7 I" D) ?, c) r- G ?3 v
- r5 ^% }) {/ w9 I" C
6 Q ~, u+ S& w3 V: GPREG_SET_ORDER撖寧摨雿 $matches[0] 銝箇洵銝蝏寥憿寧啁嚗$matches[1] 銝箇洵鈭蝏寥憿寧啁嚗隞交迨蝐餅具 - <?php
( m* W. `8 R& e$ J6 H4 | - preg_match_all ("|<[^>]+>(.*)</[^>]+>|U",
?0 X# e6 u O% } - "<b>example: </b><div align=left>this is a test</div>",1 D, \4 x" R& `( N4 u9 Q
- $out, PREG_SET_ORDER);
* X' K+ e9 f+ g" q) b6 I - print $out[0][0].", ".$out[0][1]."\n";
7 p& p) ~! p9 b) e: N - print $out[1][0].", ".$out[1][1]."\n";
, G7 ~ x2 Y3 O5 \ - ?>
銴鋆賭誨蝣 - E' f+ ~% I7 {* t% y! I# b+ `; U
砌撠颲綽 - <b>example: </b>, example:$ T+ C1 ]: ~2 n% S
- <div align=left>this is a test</div>, this is a test
, I! j$ |4 U, o9 \$ e
銴鋆賭誨蝣
+ P% Q- m0 k; r5 G6 F砌銝哨$matches[0] 舐洵銝蝏寥蝏嚗$matches[0][0] 怠寥港葵璅∪穿$matches[0][1] 怠寥蝚砌銝芸璅∪穿隞交迨蝐餅具瘀$matches[1] 舐洵鈭蝏寥蝏嚗蝑蝑 PREG_OFFSET_CAPTURE憒霈曉祆霈堆撖寞銝芸箇啁寥蝏銋嗉園撅摮蝚虫葡蝘駁瘜冽餈孵鈭餈啁潘雿踹嗡葉瘥銝芸銋臭銝芣啁嚗嗡葉蝚砌憿嫣蛹寥摮蝚虫葡嚗蝚砌憿嫣蛹嗅 subject 銝剔蝘駁祆霈啗 PHP 4.3.0 韏瑕舐具 ! f7 N G0 U8 m; \
# k$ X( l5 ^ a) n憒瘝⊥蝏箸霈堆摰銝 PREG_PATTERN_ORDER 餈港葵璅∪寥甈⊥堆航賭蛹塚嚗憒粹餈 FALSE . p/ w) W9 g$ Z7 n, L
靘摮 1. 隞砌葉敺菔瑞 - <?php
9 }& M, X1 V0 e8 Y5 Z6 d# X- w - preg_match_all ("/\(? (\d{3})? \)? (?(1) [\-\s] ) \d{3}-\d{4}/x",% p6 G$ t( w" t( z3 [
- "Call 555-1212 or 1-800-555-1212 FREE", $phones);
$ `( @+ l0 f! Q x - ?>
銴鋆賭誨蝣靘摮 2. 蝝W寥 HTML 霈堆greedy嚗 - <?php
) u+ W# n7 _4 l0 C5 \( ~; y: q* c, | - // \\2 臭銝芷撘函靘摮嚗嗅 PCRE 銝剔思! [3 p4 E7 m% W* @' {
- // 敹憿餃寥甇銵刻噢撘祈澈銝剔洵鈭蝏砍瑕摰對砌銝
% N- n- P& ~9 @) S0 _ X: K9 n - // 撠望 ([\w]+)銝箏蝚虫葡典撘瑚葉嚗隞仿閬
8 q1 Y* e6 R( Y, t - // 憭銝銝芸蝥踴- Q% s! j' L, [+ Z
- $html = "<b>bold text</b><a href=howdy.html>click me</a>";& l; j% j' n# ]7 D6 V; i
6 h4 A; ~( @9 f# {, m4 c- preg_match_all ("/(<([\w]+)[^>]*>)(.*)(<\/\\2>)/", $html, $matches);5 t7 ^- i4 Y- U& @8 Q9 x/ T
* d: G6 L; ~ ]( w4 D- for ($i=0; $i< count($matches[0]); $i++) {; ?7 U6 j Y1 |, m1 x5 G
- echo "matched: ".$matches[0][$i]."\n";
0 S2 U3 U, L3 w, Z4 n! P; f# | - echo "part 1: ".$matches[1][$i]."\n";
. c% h$ ]! |* |( d: o# ~ - echo "part 2: ".$matches[3][$i]."\n";
2 X* {3 y. t+ w! F0 u3 i - echo "part 3: ".$matches[4][$i]."\n\n";
- B5 u0 j$ a" I# S1 _( x9 [ - }) k, ^% K0 ]& M
- ?>
銴鋆賭誨蝣砌撠颲綽 - matched: <b>bold text</b>
# T1 P$ t1 x# ] - part 1: <b>* Q' X9 f5 E( w
- part 2: bold text- N6 t6 T+ @3 Y! o- \( w% l
- part 3: </b>
# Z& W2 g& A% g/ h+ x/ V - 9 s1 b& g. K) w/ x6 X W0 ~" Y
- matched: <a href=howdy.html>click me</a> ~9 X+ _4 I0 b5 L D2 J; f) [1 f; h
- part 1: <a href=howdy.html>- ]; @; {5 M1 W
- part 2: click me3 t9 a7 U$ U' {% a, E5 A
- part 3: </a>
銴鋆賭誨蝣 - P* m& m7 ] G0 h8 k
* I9 q$ V& [! t1 a$ b: U |
|