|
|
发表于 2017-12-4 00:39:19
|
显示全部楼层
本帖最后由 bjzhush 于 2017-12-4 00:45 编辑 , y5 s1 j' M8 A+ R* q
6 I* s8 V7 i9 x% x花了几分钟帮你写了下 2 s* z7 x; ^4 w$ K* C
例如源文件为这个txt
" v- P0 g: Z2 w* i5 _7 ^: m) X➜ ~ cat x.txt
- h) I* V5 k5 X& K" n2 F& lhitest
- w1 C: G- N5 L+ o4 v( o[email protected]
" V" M. o/ C$ e7 E2 Z Fhi [email protected] test
7 [' l4 u! f, K, I9 o$ f[email protected] [email protected]7 K1 D) C% n2 s. k3 t
执行后,得到result.txt
|; [6 M6 M+ c2 b➜ ~ cat result.txt 0 M. |0 j6 m2 L& b$ n
[email protected]
" v5 i1 J( q; I% I8 R) _' C5 n' O[email protected]) S2 d6 J* y; w0 G" J
[email protected]* a! d2 I& j$ {6 M: {, \
[email protected]6 l* L6 z# {* W6 P
8 C+ _8 C E- ^; ]/ k$ V3 t代码如下:" Y$ z' P/ ]; x/ R
- <?php
+ y/ W5 J& r2 ~2 _8 k d8 y8 J - $file = 'x.txt';8 \# L1 n3 C6 S4 u- \; h. O
- $fp = fopen($file, 'r');& |1 q1 H1 r8 f5 `& O
- if (!$fp) {
; D- U4 l& o) }0 Y0 d - exit('failed to open'.$file);+ W* c) N) j. J# V G
- }; ]% p0 G; d3 ]' O3 M
- $lineNumber = 0;
* R; n+ s5 _, C% K" O9 s - while($line = fgets($fp)) {* Y8 H% K1 P/ V" A n
- $lineNumber++;
+ f9 s8 T# g5 v& I4 E - $pattern = '/[a-z0-9_\-\+]+@[a-z0-9\-]+\.([a-z]{2,4})(?:\.[a-z]{2})?/i';
' @5 A8 y0 Z# S" z. @, E; j) v, b - preg_match_all($pattern, $line, $matches);
( \2 I2 Y8 W+ H) d& V: q7 [) g - if (!empty($matches[0])) {0 U; Q1 F3 \" Y, W* V
- foreach ($matches[0] as $mail) {
# S) B+ |- {" d( M4 B+ h - echo $mail.' found from line '.$lineNumber.PHP_EOL;
# E, m6 E; J( M3 ] - file_put_contents('result.txt', $mail.PHP_EOL, FILE_APPEND);
+ w V) B5 z+ T' F+ S1 } - }/ [- I6 k6 L6 I) w9 n8 u
- }
4 F8 S! b( S- y0 o - }
: l. S! z$ y: p k - echo "All Done";
4 h1 R' e( |+ Z) z
复制代码 功能:支持提取一行多个email地址,结果全部保存到result.txt ,没有做去重
& N7 W6 Q+ u, ^1 d$ a) k# T使用:代码里面的源文件是x.txt ,可以换成你自己的文件名,php保存为getMail.php 然后 php getMail.php 结果保存在result.txt里面 |
评分
-
查看全部评分
|