|
发表于 2017-12-4 00:39:19
|
显示全部楼层
本帖最后由 bjzhush 于 2017-12-4 00:45 编辑 ; A% Z; Q! h8 d C+ h& j
: T. ?! i$ U% B+ H花了几分钟帮你写了下 : m1 P( x4 g. U# } X- i
例如源文件为这个txt
8 a, a( Y) e, o8 h( ?$ Q' [➜ ~ cat x.txt
& U- f2 ?5 S7 s: Ghitest3 r! x5 ^9 I0 S
[email protected]
2 E& N" L& e2 S, Y5 ?* \7 F# ghi [email protected] test
# D- m u6 Z. q! }* V4 s[email protected] [email protected]9 }2 y( K/ X; b, s7 s: @
执行后,得到result.txt( p* d. y7 `. m! k3 R; x6 X+ m
➜ ~ cat result.txt
/ T/ R! [0 A* T8 w/ G3 R% W/ n; C[email protected]
2 m/ x# s9 |7 e[email protected]
1 J% Z) h3 }% f: k7 a1 p1 H[email protected]& h% x/ \1 F3 R6 F( w9 `
[email protected]
+ Q/ }, | k5 _) G* @
I. O+ }, x1 {4 m& `代码如下:
' R" }1 o/ K* S! y- <?php
3 o' Q' z+ M# c+ g! ?" n. A - $file = 'x.txt';
# S w/ J+ \7 J7 C2 P - $fp = fopen($file, 'r');9 J; ?2 v/ |# ]5 B! j8 l8 X
- if (!$fp) {
& X7 R/ k2 c6 S! K8 G- p1 A% ~3 I+ L - exit('failed to open'.$file);3 q& W" e4 o% O3 |3 t; G6 U- f! m
- }) O& J# r8 ?) a m2 D& ^4 L
- $lineNumber = 0;
: M) P" q3 ?4 P' \+ K* {( u - while($line = fgets($fp)) {6 s! \, |: D" }* M- q
- $lineNumber++;7 U' P& m$ `3 E, \
- $pattern = '/[a-z0-9_\-\+]+@[a-z0-9\-]+\.([a-z]{2,4})(?:\.[a-z]{2})?/i';
1 T/ L0 ]: N; l: s - preg_match_all($pattern, $line, $matches);
8 Y9 R6 F, C3 |! h' r - if (!empty($matches[0])) {1 i, J1 p9 H5 y, I7 M9 F& G
- foreach ($matches[0] as $mail) {& j+ F" i& F6 L4 I1 W
- echo $mail.' found from line '.$lineNumber.PHP_EOL;
5 _& l* A7 `. `+ `0 } - file_put_contents('result.txt', $mail.PHP_EOL, FILE_APPEND);
6 |. P; n1 y1 d' P- L4 F2 Q4 C - }
+ p, R$ T2 J% f o - }. _5 x" c+ d f7 r- ^* L( H
- }
' Z- f# d) q) [5 t - echo "All Done";/ z6 ^" b5 d. D6 C* f$ q
复制代码 功能:支持提取一行多个email地址,结果全部保存到result.txt ,没有做去重# C4 ~$ X% w5 | j/ L
使用:代码里面的源文件是x.txt ,可以换成你自己的文件名,php保存为getMail.php 然后 php getMail.php 结果保存在result.txt里面 |
评分
-
查看全部评分
|