|
发表于 2017-12-4 00:39:19
|
显示全部楼层
本帖最后由 bjzhush 于 2017-12-4 00:45 编辑 : W$ r2 K5 ?3 D+ G C
" w* q8 K1 _7 ]2 i0 ~1 f
花了几分钟帮你写了下
( N( }$ H5 b1 G5 e& p例如源文件为这个txt
# u5 T! @! b, E2 W( \) g7 n9 R➜ ~ cat x.txt" W! J- e6 Q7 z z
hitest
5 S1 p, ?& c' N8 e' A: j( U[email protected]+ @# ^# [8 g% Z. e! `' y
hi [email protected] test* H) j3 x5 _ A* _2 }/ V/ O1 d# v
[email protected] [email protected]
0 p3 A( h! e* Y1 `执行后,得到result.txt
( T6 a8 L3 _3 w ^. b$ `➜ ~ cat result.txt 3 i; g3 ^+ U( u" b- g; ~
[email protected]1 U& W- |% V% n6 L4 L9 f2 c- y
[email protected]
) e, T$ Y' f; W[email protected], Y. i1 A, n' d) c
[email protected]( S a8 [" [+ X: L, ]) }
# G9 A8 p9 D2 w
代码如下: L7 I! r; v4 Y+ Y. {7 r
- <?php
R" j3 ?, R6 R! ^# L# g b" R - $file = 'x.txt';) y) Z# W$ j4 B8 o
- $fp = fopen($file, 'r');+ o2 ]0 s. b& B8 G: a/ l. R9 E$ {
- if (!$fp) {5 {9 c3 ^8 a6 `( |' T0 S
- exit('failed to open'.$file);
8 t, [" A& H( ` - }
1 b& C. y% ^* }' d8 \* s* G - $lineNumber = 0;
( ^- K* ]6 e" N# \3 ^ - while($line = fgets($fp)) {6 k' o' R" U, `" G x, g6 h
- $lineNumber++;
1 q- Z$ C, X' F& K* R0 V - $pattern = '/[a-z0-9_\-\+]+@[a-z0-9\-]+\.([a-z]{2,4})(?:\.[a-z]{2})?/i';
! j3 Y3 E2 B. A1 k9 P8 L - preg_match_all($pattern, $line, $matches);& i9 ^4 B {$ S4 S
- if (!empty($matches[0])) {
' p$ W+ Q+ |7 D+ N3 Y - foreach ($matches[0] as $mail) {
% x/ J. e( Q. x - echo $mail.' found from line '.$lineNumber.PHP_EOL;7 X/ Y* Q/ g8 x! q- n" J4 s
- file_put_contents('result.txt', $mail.PHP_EOL, FILE_APPEND);5 H' r; c' n+ M
- }, \9 X* ?) W7 A& K1 p. j, \
- }
0 K$ \- ? N7 R& F2 \ - }4 o% s; k. t, I2 |
- echo "All Done";
! b, \/ J+ l" }+ t% x3 y8 c( i" x
复制代码 功能:支持提取一行多个email地址,结果全部保存到result.txt ,没有做去重
' \7 O) v/ i+ M1 ^使用:代码里面的源文件是x.txt ,可以换成你自己的文件名,php保存为getMail.php 然后 php getMail.php 结果保存在result.txt里面 |
评分
-
查看全部评分
|