|
int preg_match_all ( string pattern, string subject, array matches [, int flags] )
d1 u! r; m- K/ b6 G6 y0 {% U" S Y' w' q) d" Q9 f0 W! H4 v
subject 銝剜蝝Xpattern 蝏箇甇閬銵函內撘寥摰嫣蒂撠蝏隞 flags 摰摨曉 matches 銝准 蝝W啁洵銝銝芸寥銋嚗乩亦蝝W銝銝寥怠偏憪 flags 臭誑臭璅閮蝯嚗瘜冽 PREG_PATTERN_ORDER PREG_SET_ORDER 韏瑚冽瓷蝢抬嚗 6 x# A* v+ h# e1 z$ D, t
PREG_PATTERN_ORDER撠蝯摨雿 $matches[0] 箏券冽芋撘寥摮銝莎$matches[1]箇洵銝砍瑚葉摮璅∪寥摮銝莎隞交迨憿具 - <?php+ s/ J& x0 N5 l, }$ @7 T
- preg_match_all ("|<[^>]+>(.*)</[^>]+>|U",- k8 D$ |9 ?& m1 H
- "<b>example: </b><div align=left>this is a test</div>",
6 o. M5 F9 e0 K - $out, PREG_PATTERN_ORDER);
) Z g0 Q& w" l% ^! G' o8 E - print $out[0][0].", ".$out[0][1]."\n";
' N* z0 k6 y0 n* U! s+ {9 n& h, u8 n3 G - print $out[1][0].", ".$out[1][1]."\n";- n) Y3 ? o" @3 h8 u) R" H
- ?>
銴鋆賭誨蝣 ( ^. Z# i( s2 s6 y) p
砌撠颲綽 - <b>example: </b>, <div align=left>this is a test</div>. P9 {7 G U9 c6 H
- example: , this is a test
銴鋆賭誨蝣
# N8 N+ D/ w- p' Z2 X; r甇歹$out[0] 怠寥港葵璅∪摮銝莎$out[1] 思撠HTML 璅閮銋摮銝脯 # v; y ~. F0 F/ {6 k5 G
! H" v5 B$ D( E0 Q6 l& Y
( o) G0 q7 m2 l) N
PREG_SET_ORDER撖寧摨雿 $matches[0] 銝箇洵銝蝏寥憿寧啁嚗$matches[1] 銝箇洵鈭蝏寥憿寧啁嚗隞交迨蝐餅具 - <?php
0 v: w) M: [2 c/ v1 O. ~ - preg_match_all ("|<[^>]+>(.*)</[^>]+>|U",
1 a& e: |8 {* j - "<b>example: </b><div align=left>this is a test</div>",( R4 i( g. `6 U3 M
- $out, PREG_SET_ORDER);
' H; }3 c4 [" k1 c) x3 [% E - print $out[0][0].", ".$out[0][1]."\n";$ r; B7 ^) }! ^* w$ ]
- print $out[1][0].", ".$out[1][1]."\n";* g6 |$ p% f/ h- t
- ?>
銴鋆賭誨蝣
% {8 W& P) |. m: a# {' `砌撠颲綽 - <b>example: </b>, example:" C# [! A. P2 V1 }9 b* x5 C# s8 ^
- <div align=left>this is a test</div>, this is a test
1 m+ ^5 c( \' g) y7 l5 m$ w
銴鋆賭誨蝣 0 R H; s4 ?+ A2 C6 P2 t, `
砌銝哨$matches[0] 舐洵銝蝏寥蝏嚗$matches[0][0] 怠寥港葵璅∪穿$matches[0][1] 怠寥蝚砌銝芸璅∪穿隞交迨蝐餅具瘀$matches[1] 舐洵鈭蝏寥蝏嚗蝑蝑 PREG_OFFSET_CAPTURE憒霈曉祆霈堆撖寞銝芸箇啁寥蝏銋嗉園撅摮蝚虫葡蝘駁瘜冽餈孵鈭餈啁潘雿踹嗡葉瘥銝芸銋臭銝芣啁嚗嗡葉蝚砌憿嫣蛹寥摮蝚虫葡嚗蝚砌憿嫣蛹嗅 subject 銝剔蝘駁祆霈啗 PHP 4.3.0 韏瑕舐具 % {1 i+ Z9 c! A* P& n# ?1 B
& q& J; ?* T! n. w3 O0 ~% q憒瘝⊥蝏箸霈堆摰銝 PREG_PATTERN_ORDER 餈港葵璅∪寥甈⊥堆航賭蛹塚嚗憒粹餈 FALSE % m7 l! Q- V c, B% |( u! R" u
靘摮 1. 隞砌葉敺菔瑞 - <?php! X& |: t6 y. t, ^/ J' G0 O
- preg_match_all ("/\(? (\d{3})? \)? (?(1) [\-\s] ) \d{3}-\d{4}/x",
7 e% w1 m' y/ c( x# }" I) I1 Z: A - "Call 555-1212 or 1-800-555-1212 FREE", $phones);" e2 l6 c0 i( ~5 M9 O* g" x
- ?>
銴鋆賭誨蝣靘摮 2. 蝝W寥 HTML 霈堆greedy嚗 - <?php
1 u/ |5 L; C, m! s1 A - // \\2 臭銝芷撘函靘摮嚗嗅 PCRE 銝剔思 i$ B( Y+ p" |- ]' O! ]
- // 敹憿餃寥甇銵刻噢撘祈澈銝剔洵鈭蝏砍瑕摰對砌銝
7 o" g# B4 t R* O% d& t8 W* v - // 撠望 ([\w]+)銝箏蝚虫葡典撘瑚葉嚗隞仿閬% f$ s% N7 b/ b& i: v/ ~' w& i+ k
- // 憭銝銝芸蝥踴
. z) y# H/ j! |- [1 d - $html = "<b>bold text</b><a href=howdy.html>click me</a>";8 l4 v. B1 K& {' O, ]% L8 M
+ T- }# D2 _$ g- preg_match_all ("/(<([\w]+)[^>]*>)(.*)(<\/\\2>)/", $html, $matches);4 z0 s2 Y5 n0 p
- ! P$ k0 b. G( u' L0 l
- for ($i=0; $i< count($matches[0]); $i++) {" R; P* n* W1 d* q3 t7 Y3 E2 N; e; {0 @* [
- echo "matched: ".$matches[0][$i]."\n";
* s2 t5 N0 R4 h! c5 M% Y! q - echo "part 1: ".$matches[1][$i]."\n";
; H: h$ ^, a/ h4 c+ k - echo "part 2: ".$matches[3][$i]."\n";* `) ]- U4 O% u5 J
- echo "part 3: ".$matches[4][$i]."\n\n";6 h" z6 B0 \9 s% k: B9 q: e. Z: M
- }/ }2 G: ?# y" j+ H! c
- ?>
銴鋆賭誨蝣砌撠颲綽 - matched: <b>bold text</b>
- |- Q$ y$ |1 O, g - part 1: <b>
$ `. M3 N) F0 o& ]; v2 G# P - part 2: bold text
+ T, O" T/ _: G" K0 Y - part 3: </b>
, z2 I) ~+ Q* Q( S - ' j) A4 \! B; _+ s" C0 i
- matched: <a href=howdy.html>click me</a>5 [, i6 L& P7 X+ W2 p
- part 1: <a href=howdy.html>
% p3 @: U: w( d* O" _6 f% m( ^0 ] - part 2: click me6 f( m0 i- h7 `6 [: j
- part 3: </a>
銴鋆賭誨蝣 4 M2 N" }" g) s3 f0 p
8 `- g# T' `/ q( x |
|