|
int preg_match_all ( string pattern, string subject, array matches [, int flags] )0 m% J# ~9 z0 ]; P
0 Z9 w1 c& b9 Q: V) i" j subject 銝剜蝝Xpattern 蝏箇甇閬銵函內撘寥摰嫣蒂撠蝏隞 flags 摰摨曉 matches 銝准 蝝W啁洵銝銝芸寥銋嚗乩亦蝝W銝銝寥怠偏憪 flags 臭誑臭璅閮蝯嚗瘜冽 PREG_PATTERN_ORDER PREG_SET_ORDER 韏瑚冽瓷蝢抬嚗 / f( q" P& o0 I4 S# _
PREG_PATTERN_ORDER撠蝯摨雿 $matches[0] 箏券冽芋撘寥摮銝莎$matches[1]箇洵銝砍瑚葉摮璅∪寥摮銝莎隞交迨憿具 - <?php, d1 P8 `. r9 O
- preg_match_all ("|<[^>]+>(.*)</[^>]+>|U",
& I$ [" X; [6 y) K; ]6 ]! N - "<b>example: </b><div align=left>this is a test</div>",
9 {( d. A1 F8 v1 h7 `9 e - $out, PREG_PATTERN_ORDER);
; {8 o' r6 v0 i1 P6 N# m9 I - print $out[0][0].", ".$out[0][1]."\n";
2 w5 z0 h, l5 S: _ - print $out[1][0].", ".$out[1][1]."\n";
, `, }2 D2 H& u# S1 }8 V4 _: G - ?>
銴鋆賭誨蝣
. @4 u# O+ C) ^7 w' ?砌撠颲綽 - <b>example: </b>, <div align=left>this is a test</div># k" \% V7 K0 T% O9 y% Z/ G! f5 m
- example: , this is a test
銴鋆賭誨蝣 % @$ f- V5 j0 |) N( T
甇歹$out[0] 怠寥港葵璅∪摮銝莎$out[1] 思撠HTML 璅閮銋摮銝脯
4 l' k* D5 Q g, X6 x
* b: J% x8 P$ o; a
' H( J* |) |) F+ X: |PREG_SET_ORDER撖寧摨雿 $matches[0] 銝箇洵銝蝏寥憿寧啁嚗$matches[1] 銝箇洵鈭蝏寥憿寧啁嚗隞交迨蝐餅具 - <?php$ v5 y3 V& N2 L. `
- preg_match_all ("|<[^>]+>(.*)</[^>]+>|U",
* X3 d, {. y1 f6 V6 U - "<b>example: </b><div align=left>this is a test</div>",$ Z. \' a( [! V( w. b9 t
- $out, PREG_SET_ORDER);1 f# S: J5 i( Y! `$ }
- print $out[0][0].", ".$out[0][1]."\n";+ {0 e2 f, \5 A
- print $out[1][0].", ".$out[1][1]."\n";
2 u# H# y% ~7 {4 M+ \4 W* @' N - ?>
銴鋆賭誨蝣 * y6 X M" d' t6 P5 A/ M
砌撠颲綽 - <b>example: </b>, example:6 I# y+ b( i6 C" h
- <div align=left>this is a test</div>, this is a test
V) x2 ]) G7 N$ W" M0 M
銴鋆賭誨蝣 6 B7 `# J: `' q
砌銝哨$matches[0] 舐洵銝蝏寥蝏嚗$matches[0][0] 怠寥港葵璅∪穿$matches[0][1] 怠寥蝚砌銝芸璅∪穿隞交迨蝐餅具瘀$matches[1] 舐洵鈭蝏寥蝏嚗蝑蝑 PREG_OFFSET_CAPTURE憒霈曉祆霈堆撖寞銝芸箇啁寥蝏銋嗉園撅摮蝚虫葡蝘駁瘜冽餈孵鈭餈啁潘雿踹嗡葉瘥銝芸銋臭銝芣啁嚗嗡葉蝚砌憿嫣蛹寥摮蝚虫葡嚗蝚砌憿嫣蛹嗅 subject 銝剔蝘駁祆霈啗 PHP 4.3.0 韏瑕舐具 1 r8 \# _7 n4 `- P
& V% E. Q T; H3 w( y: G% e$ i憒瘝⊥蝏箸霈堆摰銝 PREG_PATTERN_ORDER 餈港葵璅∪寥甈⊥堆航賭蛹塚嚗憒粹餈 FALSE
6 @1 R: B- }+ Y! z$ ~3 M' O, J靘摮 1. 隞砌葉敺菔瑞 - <?php
9 D( ]% A5 j' s1 @ - preg_match_all ("/\(? (\d{3})? \)? (?(1) [\-\s] ) \d{3}-\d{4}/x",
! m) O1 m* Q" W# H* X1 K* R - "Call 555-1212 or 1-800-555-1212 FREE", $phones);6 S4 x" b$ w# g+ O& X
- ?>
銴鋆賭誨蝣靘摮 2. 蝝W寥 HTML 霈堆greedy嚗 - <?php
; i% Y/ c3 |9 g" A: _ - // \\2 臭銝芷撘函靘摮嚗嗅 PCRE 銝剔思
2 |% Q2 V) ]0 h+ U6 g; Y - // 敹憿餃寥甇銵刻噢撘祈澈銝剔洵鈭蝏砍瑕摰對砌銝+ c2 l) B% o4 K) O. [8 M' ?
- // 撠望 ([\w]+)銝箏蝚虫葡典撘瑚葉嚗隞仿閬' B5 z6 q, s/ H5 ]( D, c
- // 憭銝銝芸蝥踴( t2 `! s' y/ F3 r9 n2 p! ]
- $html = "<b>bold text</b><a href=howdy.html>click me</a>";
6 `' I/ P( Z4 V) S0 T1 e4 Q
( t# E/ _! \& U- preg_match_all ("/(<([\w]+)[^>]*>)(.*)(<\/\\2>)/", $html, $matches);
7 {4 X6 h, U) {( n+ R& O
8 ?+ s$ q( t& a* J6 \8 r$ ~" j* v- for ($i=0; $i< count($matches[0]); $i++) {
) }7 v/ \/ D6 p# C/ R: J - echo "matched: ".$matches[0][$i]."\n";
* O$ ?$ U5 F' M. F W, l - echo "part 1: ".$matches[1][$i]."\n";/ _* {4 e. Y. Q& F: }7 [
- echo "part 2: ".$matches[3][$i]."\n";
$ z5 b+ j* m+ U! {4 z' p/ i3 p - echo "part 3: ".$matches[4][$i]."\n\n";
) i8 U$ C0 h6 h! a1 T - }: _2 k" i1 l% E* l% n, a0 I( U
- ?>
銴鋆賭誨蝣砌撠颲綽 - matched: <b>bold text</b>
& L5 Q! O% l+ r( B$ ] - part 1: <b>
9 D% [( j8 A6 J, r9 ]: _; p% W9 L - part 2: bold text* t9 N5 _: C( }6 s, q& W, i# t
- part 3: </b>. Y w5 T; ^ D- C: I4 O* U
- - S9 L( d- v; y& ^ x+ Z: Q9 e
- matched: <a href=howdy.html>click me</a>
7 Y% F! g% k1 p5 |( E - part 1: <a href=howdy.html>
8 x; S; I$ B/ l& o/ n( [ - part 2: click me
$ N6 Q7 b% X7 h4 ^3 @6 ?+ S - part 3: </a>
銴鋆賭誨蝣
7 C1 S& s9 r% f, q: z$ c( s L7 E/ b1 A) g, y
|
|