|
|
发表于 2017-12-4 00:39:19
|
显示全部楼层
本帖最后由 bjzhush 于 2017-12-4 00:45 编辑 9 @- f7 `3 t$ w) q A3 K0 ]9 H$ h
9 V- ~& e9 W7 M; v8 N花了几分钟帮你写了下
4 n5 E5 w! y7 s" e6 a例如源文件为这个txt: z: t; Z3 j. }; x
➜ ~ cat x.txt
- }0 \5 a4 U' _* _- Fhitest
3 D' N* J$ H: b$ U& A, O/ E[email protected]1 }# f+ B* L$ _" J1 d& G
hi [email protected] test$ K( K- o: V) H$ k5 h6 A" P3 o
[email protected] [email protected]; o4 P7 p% J: g* }; L+ n
执行后,得到result.txt
! l" R# O; d, q) P8 [8 w: q➜ ~ cat result.txt 6 x# v; X+ t$ s' Q! d- M6 G. q
[email protected]! }7 X* o. }. ?; f* A: C6 w( a& C0 D
[email protected]
: b0 \* O1 m/ r1 r) m: [, K9 J[email protected]
; N! D% s) k: U+ m$ L6 q[email protected]
0 ^" Y' l: K! M9 u7 l6 K' x8 d& u! N. L3 i9 o1 V! k* q
代码如下: f: V! ~: l8 o
- <?php
5 S* @* B- Y4 |4 T: a( |9 P - $file = 'x.txt';/ m- G; S% q# h* ]# E% ?( I
- $fp = fopen($file, 'r');& L% C v0 L% z& \5 _" m& I
- if (!$fp) {
' I% i1 Z) n% Y& j- T - exit('failed to open'.$file);
& g- G- Z _- C% s3 y - }6 v7 c. ~/ j; A( ~
- $lineNumber = 0;
- u' b; k( [: f( y( b - while($line = fgets($fp)) {
& I( ^ C8 p# H% d. W& Y - $lineNumber++;6 m0 ^. T* V$ d6 U
- $pattern = '/[a-z0-9_\-\+]+@[a-z0-9\-]+\.([a-z]{2,4})(?:\.[a-z]{2})?/i';. K7 o, j' V. J
- preg_match_all($pattern, $line, $matches);
# T Z* w" P, @! r" \ - if (!empty($matches[0])) {
5 S/ d$ n1 l! W* D& \ - foreach ($matches[0] as $mail) {% D8 O' U' ]4 r/ G: P" J$ \1 l
- echo $mail.' found from line '.$lineNumber.PHP_EOL;' `! l2 A3 M* i6 `& J' f- P1 r
- file_put_contents('result.txt', $mail.PHP_EOL, FILE_APPEND);* b9 ^( N9 H D0 S
- }
: r b2 ?/ e D0 I# N+ t - }
& B7 ]3 o V3 ?5 m K4 H - } ?; W. C5 D) r
- echo "All Done";: J' x) p* Y% S/ \
复制代码 功能:支持提取一行多个email地址,结果全部保存到result.txt ,没有做去重
" I5 K3 H- ^$ ?使用:代码里面的源文件是x.txt ,可以换成你自己的文件名,php保存为getMail.php 然后 php getMail.php 结果保存在result.txt里面 |
评分
-
查看全部评分
|