|
|
发表于 2017-12-4 00:39:19
|
显示全部楼层
本帖最后由 bjzhush 于 2017-12-4 00:45 编辑 1 g( a: ^! F4 c3 n* C T* c. e
( x% q: e) T. t6 i+ \4 H7 `8 s
花了几分钟帮你写了下
9 b9 K3 @! l7 P! y# _# t, u例如源文件为这个txt
' J' v* ?/ L0 |% X( w8 Z8 c➜ ~ cat x.txt j" S i$ V( T
hitest* V+ r* m$ K0 g7 U% {, V! a
[email protected]1 w; y/ ?* \$ d$ H8 `2 l! a
hi [email protected] test
0 A/ p! H+ g0 D& u4 E[email protected] [email protected]
+ \- u7 q8 N: {. c执行后,得到result.txt
( c% d% ~- l+ U( `2 \➜ ~ cat result.txt : A5 q1 N. h4 D" Q) p( C4 V6 R
[email protected]7 c. ]$ K& N4 q
[email protected]9 T5 D3 A2 Y& d1 B8 m) G% g( U9 c
[email protected]
# R# @7 ~ I3 x* Y0 ?[email protected]
& W/ @- G2 {' j: h3 ?) ]( d* L
4 u9 t8 R* J# k# Q+ u p代码如下:! d$ C2 I- M: Z( b6 |4 U
- <?php% {) C0 j- e# ]& ]
- $file = 'x.txt';
- x8 s/ ]( H6 G% Z* f* p8 l - $fp = fopen($file, 'r');5 P- r9 \' z& d8 D z" P
- if (!$fp) {. `# M9 b& E. _
- exit('failed to open'.$file);
0 S; x: I$ {6 }2 Y1 ^3 w - }
9 `6 @; S! q2 L3 J3 Q6 d T - $lineNumber = 0;
" `; e" N4 C3 a8 T9 Z, X - while($line = fgets($fp)) {& h* l0 ^1 T5 q
- $lineNumber++;
* e( w, P# s! Q! N4 I1 [4 |6 O4 n - $pattern = '/[a-z0-9_\-\+]+@[a-z0-9\-]+\.([a-z]{2,4})(?:\.[a-z]{2})?/i';
$ t. |8 t: I' V/ A K - preg_match_all($pattern, $line, $matches);$ ?% b3 |: J6 X9 S2 W& ^2 V
- if (!empty($matches[0])) {. H N' [( b* x. _! } `
- foreach ($matches[0] as $mail) {' w2 g% P" r O+ G5 k
- echo $mail.' found from line '.$lineNumber.PHP_EOL;
; z8 o: t7 Y: Q2 e - file_put_contents('result.txt', $mail.PHP_EOL, FILE_APPEND);; o+ \7 L4 [# ?. `
- }, g. c, W: ?0 E7 K- Q) S
- }
# S; a% e, _% g) ]1 O - }. p$ f9 @! V, ~6 V' O, H) M
- echo "All Done";
8 r! k& m5 E0 r: }. A# e
复制代码 功能:支持提取一行多个email地址,结果全部保存到result.txt ,没有做去重$ q# k3 M* }2 Q* t3 u
使用:代码里面的源文件是x.txt ,可以换成你自己的文件名,php保存为getMail.php 然后 php getMail.php 结果保存在result.txt里面 |
评分
-
查看全部评分
|