int preg_match_all ( string pattern, string subject, array matches [, int flags] )
2 Q, f2 Q' o% e+ V; R! U% N; f
; l) v/ h) l7 w subject 銝剜蝝Xpattern 蝏箇甇閬銵函內撘寥摰嫣蒂撠蝏隞 flags 摰摨曉 matches 銝准 蝝W啁洵銝銝芸寥銋嚗乩亦蝝W銝銝寥怠偏憪 flags 臭誑臭璅閮蝯嚗瘜冽 PREG_PATTERN_ORDER PREG_SET_ORDER 韏瑚冽瓷蝢抬嚗 5 n# P& {, z' B# V# T# d N
PREG_PATTERN_ORDER撠蝯摨雿 $matches[0] 箏券冽芋撘寥摮銝莎$matches[1]箇洵銝砍瑚葉摮璅∪寥摮銝莎隞交迨憿具 - <?php
1 D/ n" E6 y$ x4 T$ k5 N! } - preg_match_all ("|<[^>]+>(.*)</[^>]+>|U",
3 ^ b& C7 @% E8 |4 ] - "<b>example: </b><div align=left>this is a test</div>",# ~3 I0 K( X3 ~
- $out, PREG_PATTERN_ORDER);: ?9 R: x v0 ^/ C* ]
- print $out[0][0].", ".$out[0][1]."\n";" q' q0 `+ T1 I0 l' ~
- print $out[1][0].", ".$out[1][1]."\n";& {' I/ Q* v4 B( e+ q3 Y# M$ G
- ?>
銴鋆賭誨蝣
" Y6 {( k$ { s/ S1 d+ H砌撠颲綽 - <b>example: </b>, <div align=left>this is a test</div>
5 h2 s5 t9 o/ e! t0 w! K - example: , this is a test
銴鋆賭誨蝣
/ |: F$ ~, [) V6 l甇歹$out[0] 怠寥港葵璅∪摮銝莎$out[1] 思撠HTML 璅閮銋摮銝脯
% {# d! i0 f5 T5 c' a2 u
`7 h; g/ Q- }- U
1 }* I0 Z: s/ \, \; x- z- BPREG_SET_ORDER撖寧摨雿 $matches[0] 銝箇洵銝蝏寥憿寧啁嚗$matches[1] 銝箇洵鈭蝏寥憿寧啁嚗隞交迨蝐餅具 - <?php
) y1 T( G/ G. V0 V - preg_match_all ("|<[^>]+>(.*)</[^>]+>|U",
( l' H5 S6 v4 O# w% \; U) t - "<b>example: </b><div align=left>this is a test</div>",$ Y/ q* J9 k) L- A4 u
- $out, PREG_SET_ORDER);
- q6 Y0 | z4 l$ Q* F7 P; r$ h( S( L; s, j - print $out[0][0].", ".$out[0][1]."\n";
+ S' m' `# N2 ^; V - print $out[1][0].", ".$out[1][1]."\n";
* C l6 e1 J( L N. v - ?>
銴鋆賭誨蝣 8 b; S. f3 p: I/ X/ \
砌撠颲綽 - <b>example: </b>, example:! F; ^9 h/ ?% b1 |, G
- <div align=left>this is a test</div>, this is a test
/ d% N# `8 K @" ~ _6 c- B# ^
銴鋆賭誨蝣
. v6 g! c6 h! Z( u砌銝哨$matches[0] 舐洵銝蝏寥蝏嚗$matches[0][0] 怠寥港葵璅∪穿$matches[0][1] 怠寥蝚砌銝芸璅∪穿隞交迨蝐餅具瘀$matches[1] 舐洵鈭蝏寥蝏嚗蝑蝑 PREG_OFFSET_CAPTURE憒霈曉祆霈堆撖寞銝芸箇啁寥蝏銋嗉園撅摮蝚虫葡蝘駁瘜冽餈孵鈭餈啁潘雿踹嗡葉瘥銝芸銋臭銝芣啁嚗嗡葉蝚砌憿嫣蛹寥摮蝚虫葡嚗蝚砌憿嫣蛹嗅 subject 銝剔蝘駁祆霈啗 PHP 4.3.0 韏瑕舐具 7 l6 c7 O1 q# D A6 O: l
7 K# o8 A1 Q$ t) x! O9 u5 c憒瘝⊥蝏箸霈堆摰銝 PREG_PATTERN_ORDER 餈港葵璅∪寥甈⊥堆航賭蛹塚嚗憒粹餈 FALSE
! X: p! T: u" M( R1 `- w3 R靘摮 1. 隞砌葉敺菔瑞 - <?php2 e0 l4 m+ \% Z- _; |( u, O
- preg_match_all ("/\(? (\d{3})? \)? (?(1) [\-\s] ) \d{3}-\d{4}/x",
& `& D$ {5 C. R4 K - "Call 555-1212 or 1-800-555-1212 FREE", $phones);. n. P Z) A+ j- Y
- ?>
銴鋆賭誨蝣靘摮 2. 蝝W寥 HTML 霈堆greedy嚗 - <?php' }5 ^2 P Z# @; a e% v
- // \\2 臭銝芷撘函靘摮嚗嗅 PCRE 銝剔思
) p& T( m% ]# X4 M) a) b - // 敹憿餃寥甇銵刻噢撘祈澈銝剔洵鈭蝏砍瑕摰對砌銝2 Z8 _3 T2 X' x9 |2 K0 j* e2 e, ]
- // 撠望 ([\w]+)銝箏蝚虫葡典撘瑚葉嚗隞仿閬, L$ p# G5 A! O: I
- // 憭銝銝芸蝥踴; q& ^ Y: r. p) h B% V
- $html = "<b>bold text</b><a href=howdy.html>click me</a>";6 p8 r1 h# U0 z+ q; M4 q+ F& i
, W" U! f# y0 D- preg_match_all ("/(<([\w]+)[^>]*>)(.*)(<\/\\2>)/", $html, $matches);" y R6 u$ [6 e* N8 e" ?4 D
, v# E! u j& b3 k; p- for ($i=0; $i< count($matches[0]); $i++) {9 @: x2 D0 K8 }0 ~: H; d2 p- i+ W
- echo "matched: ".$matches[0][$i]."\n";- e4 P; B8 H4 A7 j" Q* C
- echo "part 1: ".$matches[1][$i]."\n";" {% O# I1 Z9 b) Y8 n0 l
- echo "part 2: ".$matches[3][$i]."\n";
& A7 U2 N/ T- E9 ^! k% X - echo "part 3: ".$matches[4][$i]."\n\n";8 Q7 a8 W' I- Z* O* X3 r% O9 l
- }5 e- E7 K& c( B5 n/ U' ]
- ?>
銴鋆賭誨蝣砌撠颲綽 - matched: <b>bold text</b>% E, o9 ~) Z9 h. l. w; C9 h
- part 1: <b>" m8 q8 G. I' C, T9 K+ e" J, B
- part 2: bold text: K2 p' j& i# q
- part 3: </b>0 ^& F- B( Q" e ~1 @1 V
- " D0 }5 N/ l5 J9 Q8 R/ ~: d4 S
- matched: <a href=howdy.html>click me</a>
i- l1 F" ]3 D' k' X$ ~ - part 1: <a href=howdy.html>
4 T' ]' ?0 O+ ?8 }; t- Z* o! ? - part 2: click me
$ v: ~6 i, U' {3 i- M7 P& c - part 3: </a>
銴鋆賭誨蝣
p; v3 m" y' j& F5 J/ R' z' @& l
|
|