int preg_match_all ( string pattern, string subject, array matches [, int flags] )
' G9 T- R; L+ h6 i8 S6 L7 ~. V& q8 u) t( Q
subject 銝剜蝝Xpattern 蝏箇甇閬銵函內撘寥摰嫣蒂撠蝏隞 flags 摰摨曉 matches 銝准 蝝W啁洵銝銝芸寥銋嚗乩亦蝝W銝銝寥怠偏憪 flags 臭誑臭璅閮蝯嚗瘜冽 PREG_PATTERN_ORDER PREG_SET_ORDER 韏瑚冽瓷蝢抬嚗
$ F- }. }( @0 N2 bPREG_PATTERN_ORDER撠蝯摨雿 $matches[0] 箏券冽芋撘寥摮銝莎$matches[1]箇洵銝砍瑚葉摮璅∪寥摮銝莎隞交迨憿具 - <?php
7 x0 a; M V. u - preg_match_all ("|<[^>]+>(.*)</[^>]+>|U",
, X" J+ e* K/ V3 o, ]' G% a$ s - "<b>example: </b><div align=left>this is a test</div>",5 D/ k+ j8 \, P8 K$ i" B
- $out, PREG_PATTERN_ORDER);
$ k/ p# H* Z& m9 J0 P- m - print $out[0][0].", ".$out[0][1]."\n";
3 a% B E* f1 z$ a - print $out[1][0].", ".$out[1][1]."\n"; ]0 d' o+ {7 O; I9 g0 n
- ?>
銴鋆賭誨蝣 7 Q( I; B2 Z7 h: [3 M
砌撠颲綽 - <b>example: </b>, <div align=left>this is a test</div>
, W J# V+ t5 W - example: , this is a test
銴鋆賭誨蝣
$ n. Z2 T, e! @4 Y甇歹$out[0] 怠寥港葵璅∪摮銝莎$out[1] 思撠HTML 璅閮銋摮銝脯
* X: e0 K7 i! X: B8 i, |# ?
9 q. p& D; g+ C# t6 m$ x2 A4 S9 d; ~( R, d3 a6 O9 h4 Q6 U& W
PREG_SET_ORDER撖寧摨雿 $matches[0] 銝箇洵銝蝏寥憿寧啁嚗$matches[1] 銝箇洵鈭蝏寥憿寧啁嚗隞交迨蝐餅具 - <?php/ T J( E# o: Z, I9 k* s' k. l ]
- preg_match_all ("|<[^>]+>(.*)</[^>]+>|U",6 _# L! f' N: V9 X; I `
- "<b>example: </b><div align=left>this is a test</div>",
$ V/ F# ]8 z0 Q1 u \& ` o( R - $out, PREG_SET_ORDER);
+ D' k; S! a. m( N - print $out[0][0].", ".$out[0][1]."\n";
6 E4 p) \& G" Z; k3 H) s - print $out[1][0].", ".$out[1][1]."\n";
2 B* G8 e- ?3 c% @# R - ?>
銴鋆賭誨蝣
9 J2 |- \3 R3 y5 S0 Q& r砌撠颲綽 - <b>example: </b>, example:$ t, B, \- t& M v& N6 Q+ e
- <div align=left>this is a test</div>, this is a test) M9 x2 L: g- E" I, i) u1 P
銴鋆賭誨蝣 ' |5 C/ I4 {% x' k Q2 v7 j/ S: M- e
砌銝哨$matches[0] 舐洵銝蝏寥蝏嚗$matches[0][0] 怠寥港葵璅∪穿$matches[0][1] 怠寥蝚砌銝芸璅∪穿隞交迨蝐餅具瘀$matches[1] 舐洵鈭蝏寥蝏嚗蝑蝑 PREG_OFFSET_CAPTURE憒霈曉祆霈堆撖寞銝芸箇啁寥蝏銋嗉園撅摮蝚虫葡蝘駁瘜冽餈孵鈭餈啁潘雿踹嗡葉瘥銝芸銋臭銝芣啁嚗嗡葉蝚砌憿嫣蛹寥摮蝚虫葡嚗蝚砌憿嫣蛹嗅 subject 銝剔蝘駁祆霈啗 PHP 4.3.0 韏瑕舐具
; e) H8 c$ I9 ]- N9 S, p' D- z* k. ~
憒瘝⊥蝏箸霈堆摰銝 PREG_PATTERN_ORDER 餈港葵璅∪寥甈⊥堆航賭蛹塚嚗憒粹餈 FALSE 8 w3 F/ a) W* [% @- J
靘摮 1. 隞砌葉敺菔瑞 - <?php
% ~' N$ x0 [( {! _ - preg_match_all ("/\(? (\d{3})? \)? (?(1) [\-\s] ) \d{3}-\d{4}/x",
- y! R5 x$ b: S* d# I" W - "Call 555-1212 or 1-800-555-1212 FREE", $phones);/ S8 R* b6 ~) ~5 F3 B8 P
- ?>
銴鋆賭誨蝣靘摮 2. 蝝W寥 HTML 霈堆greedy嚗 - <?php* M1 S' Z) u! m
- // \\2 臭銝芷撘函靘摮嚗嗅 PCRE 銝剔思5 p p! D1 U( e$ Y F
- // 敹憿餃寥甇銵刻噢撘祈澈銝剔洵鈭蝏砍瑕摰對砌銝
- O5 M- V" A$ Z% { M5 m - // 撠望 ([\w]+)銝箏蝚虫葡典撘瑚葉嚗隞仿閬
6 A2 r- O# `) }9 [- _, m - // 憭銝銝芸蝥踴
: F' O q/ _6 h2 f! [! H# [ - $html = "<b>bold text</b><a href=howdy.html>click me</a>";& Z. ~* ]6 j7 J) m
- 9 K' x5 u" M6 _& W3 e. U
- preg_match_all ("/(<([\w]+)[^>]*>)(.*)(<\/\\2>)/", $html, $matches);' g$ l \. O, G* q# Y
, g% d7 r% t+ Y' x# S3 Q- for ($i=0; $i< count($matches[0]); $i++) {
$ B+ k9 |& Q) B. r: m H+ i5 F - echo "matched: ".$matches[0][$i]."\n";
7 T4 G5 `7 ]3 Q3 M - echo "part 1: ".$matches[1][$i]."\n";5 B7 ]1 n' }! Q- G
- echo "part 2: ".$matches[3][$i]."\n";
[' P* c2 y- w. T - echo "part 3: ".$matches[4][$i]."\n\n";4 k: S: j0 w0 q- f3 A* K
- }% ]+ ^: e! c5 U3 ?+ G; U
- ?>
銴鋆賭誨蝣砌撠颲綽 - matched: <b>bold text</b>, u: w& I9 b! r
- part 1: <b>
& l+ D& _* |, {: P, \. Z o - part 2: bold text
7 J) P& d4 d& A1 s. J! ]8 x - part 3: </b>
& {! v+ |* L( |7 b8 \2 E - & U& X2 c/ K% \, y- T
- matched: <a href=howdy.html>click me</a>
3 P. p4 Y0 Q$ u& \* j4 ] - part 1: <a href=howdy.html>7 n% b0 p6 X7 G$ a! G; \
- part 2: click me
6 r1 r- m/ V8 g# `& y6 X - part 3: </a>
銴鋆賭誨蝣
@0 K: W7 o) P, O8 I' `
5 y- ~7 g0 V* q: d |
|