|
|
发表于 2017-12-4 00:39:19
|
显示全部楼层
本帖最后由 bjzhush 于 2017-12-4 00:45 编辑 " r1 k; |$ |2 G, J; y
, t) m3 f& {& X7 k, y5 C花了几分钟帮你写了下
* r& i: Q/ B8 F- |; z例如源文件为这个txt3 f9 V7 Y4 s8 ^' Q
➜ ~ cat x.txt9 J f/ |; z0 s, h h- v! m
hitest
" |9 l2 b% V$ k5 H9 r; ^1 Y[email protected]
9 w' j( D3 T+ [4 c+ Yhi [email protected] test
4 s5 V/ I# B) Z/ A& V# Q5 A[email protected] [email protected]. }) ]5 u1 a6 T8 a
执行后,得到result.txt
, p% B+ }8 J% M➜ ~ cat result.txt
. V5 ]2 W, X# v+ U9 ^1 u[email protected]& ?1 a) M/ Z' t
[email protected] q- Q3 C4 F) y/ V
[email protected]1 S7 P$ y; O9 E) ^
[email protected]
- @ i9 w5 v/ e& Y- p! c& V8 T% ]4 t* a* Q
, Y7 t# a9 t5 `; ^( E# R代码如下:5 G+ n L" {6 l$ Y, t A T
- <?php
' T" A+ x) a8 k7 }5 T0 u - $file = 'x.txt';) Q |0 B6 ?- l& z6 s% @
- $fp = fopen($file, 'r');
. S3 d4 }; H) l3 m' z - if (!$fp) {
: J4 |0 { p' V1 b - exit('failed to open'.$file);
4 z% H* [* Z+ d- d - }
0 _( H# r" H+ p: I) h% Q - $lineNumber = 0;" m9 W6 t4 }$ T+ G/ |7 T( m6 y
- while($line = fgets($fp)) {
. [/ T+ ~: Z B( X - $lineNumber++;- X( y! \9 O6 z3 [4 ^% F
- $pattern = '/[a-z0-9_\-\+]+@[a-z0-9\-]+\.([a-z]{2,4})(?:\.[a-z]{2})?/i';$ a1 A p1 X4 |) a$ g, H
- preg_match_all($pattern, $line, $matches);# Q4 t+ M0 u- ?+ T6 U
- if (!empty($matches[0])) {6 Z2 Y6 \; i" j2 c8 N% _5 l
- foreach ($matches[0] as $mail) {
8 [) S2 f3 l s& I6 ~/ M - echo $mail.' found from line '.$lineNumber.PHP_EOL;
- z$ I2 l. g( |* i. J4 K3 D Z - file_put_contents('result.txt', $mail.PHP_EOL, FILE_APPEND);
) p' m& [1 n! S0 ] - }
" A/ Y3 s* k: `, X# t2 @ - }- |9 s" z! t( q# K8 h8 j' C
- }) C% M$ |- D/ i, J7 @
- echo "All Done";( X# i/ R- y4 J, E) k# G# |
复制代码 功能:支持提取一行多个email地址,结果全部保存到result.txt ,没有做去重
, f7 R0 o5 r7 x使用:代码里面的源文件是x.txt ,可以换成你自己的文件名,php保存为getMail.php 然后 php getMail.php 结果保存在result.txt里面 |
评分
-
查看全部评分
|