|
|
发表于 2017-12-4 00:39:19
|
显示全部楼层
本帖最后由 bjzhush 于 2017-12-4 00:45 编辑
$ W: |/ p5 S5 N1 P2 D6 E U( s, q3 D5 t( y
花了几分钟帮你写了下
( M( x; t! T V6 N+ z/ w例如源文件为这个txt5 b7 z. S( X4 {) [
➜ ~ cat x.txt' Z+ J9 T, p8 w, g1 v
hitest+ Q" e# u. Z! G7 r) Z3 P$ s, J
[email protected]
& d& Y V4 E. {3 B$ \& qhi [email protected] test1 j. b8 X, a2 {" \$ v
[email protected] [email protected]7 ^5 q: c! c/ E) R$ P7 S
执行后,得到result.txt
v! @% Q* U+ r _) K➜ ~ cat result.txt 8 ?4 G' N& P7 R* @: E
[email protected]9 h' v* Q; U( z Y* V1 Q6 u9 {2 s
[email protected]1 y! ~! y1 W& ]2 K+ S
[email protected]. d5 k6 W+ J# _" T# O7 W
[email protected]
7 f& U4 r2 j( K/ y7 Y# V
( T- J/ ]" e# t( ?; Z+ K代码如下:
/ | V' O% C: X( Q* Y8 e8 c- <?php/ ?3 M& j! I9 U# k& n5 v" d9 \
- $file = 'x.txt'; t( @/ G* n4 \' O; A4 l" P% j
- $fp = fopen($file, 'r');+ ]+ w$ F q; n I" d/ _, h+ b
- if (!$fp) {3 x7 E8 h7 z/ L. ~+ ~
- exit('failed to open'.$file);
6 [/ {2 Y& C: ]+ W( H: U - }2 U; P$ b6 c8 ]' B3 p- |' |* S$ U; \- k
- $lineNumber = 0;4 o, `7 i+ k9 j* j
- while($line = fgets($fp)) {/ }/ s4 r* @. a
- $lineNumber++;
: F( S6 |7 E7 ~; L! X4 W/ ^/ f - $pattern = '/[a-z0-9_\-\+]+@[a-z0-9\-]+\.([a-z]{2,4})(?:\.[a-z]{2})?/i';- }5 r6 A, [2 k# _
- preg_match_all($pattern, $line, $matches);. ^% j6 T- K" U- V7 v% j$ {
- if (!empty($matches[0])) {8 c6 f b* N1 v& q% {
- foreach ($matches[0] as $mail) {
4 s7 g& M2 D. v' [( s! X8 F - echo $mail.' found from line '.$lineNumber.PHP_EOL;
* e( U: W( ~5 | - file_put_contents('result.txt', $mail.PHP_EOL, FILE_APPEND);
1 c& H5 m+ {7 l0 W- }. ~% v - }
2 G5 u+ X6 \% s, X: j7 t* e - }
2 M5 R' _+ f9 f; e' k C: u7 [ - }
0 t/ \# [$ ^1 O W) M - echo "All Done";
1 h$ U4 R5 m. ~* O' l M: i
复制代码 功能:支持提取一行多个email地址,结果全部保存到result.txt ,没有做去重
# }1 q. t4 [& T/ V5 J7 M/ z使用:代码里面的源文件是x.txt ,可以换成你自己的文件名,php保存为getMail.php 然后 php getMail.php 结果保存在result.txt里面 |
评分
-
查看全部评分
|