|
发表于 2017-12-4 00:39:19
|
显示全部楼层
本帖最后由 bjzhush 于 2017-12-4 00:45 编辑
. q' I+ M" l, O) i5 O7 W( h" b4 P
花了几分钟帮你写了下 $ e' @$ {9 _' h
例如源文件为这个txt
$ O6 Y. G7 Z4 L➜ ~ cat x.txt
O& z0 n2 ]' i$ `hitest& N0 B) Y3 s. [7 L# _! e
[email protected]2 O3 F c% W, E9 {) D
hi [email protected] test
4 K9 k1 l0 G( G+ n& U4 y! j$ e[email protected] [email protected]
: P7 N8 c: v6 o1 `执行后,得到result.txt' U. c" j+ _# K# `/ G- j
➜ ~ cat result.txt
7 k5 V- l5 Q$ _4 i6 E d( i# ^[email protected]
1 s% T- s, d$ `. @- q[email protected]
4 l- u. y9 b4 I[email protected]
5 X/ f. P9 W( N- k[email protected]
U3 ~8 c, N* [5 \& k! N; F0 F" @- k8 A7 p- _* N, y
代码如下:- \/ O/ w. T2 Q+ c
- <?php
5 y4 m2 k7 v+ d% l L3 t, ]/ b - $file = 'x.txt';
) k& W* y4 |" C - $fp = fopen($file, 'r');- m8 t8 V; D. O! t5 y
- if (!$fp) {
* g6 f% Q; s* Q9 u% ]. G) n9 I - exit('failed to open'.$file); h$ \1 T" c; q( V0 F9 z
- }
Z. g: I; C( \ - $lineNumber = 0;
2 R" Y( q2 t2 e! G% i: H - while($line = fgets($fp)) {
q- g, s A6 g: d4 H) i! L* C - $lineNumber++;
1 n' ~0 | L8 B" d5 b' J1 Q3 U - $pattern = '/[a-z0-9_\-\+]+@[a-z0-9\-]+\.([a-z]{2,4})(?:\.[a-z]{2})?/i';( l, h B# D3 `% t) U7 @
- preg_match_all($pattern, $line, $matches); r- N7 n6 }3 t" v: `
- if (!empty($matches[0])) {
1 y4 I5 l/ q. U/ U" \4 }' I* V - foreach ($matches[0] as $mail) {) |# ^( }+ \8 M: ~! d6 g* K: g8 a
- echo $mail.' found from line '.$lineNumber.PHP_EOL;$ z1 |3 E" u8 a( ?. s9 Q0 q
- file_put_contents('result.txt', $mail.PHP_EOL, FILE_APPEND);
" `1 H( d$ f: k$ r7 d0 G b; N - }9 Q" X, A' K0 |8 {* e2 c
- }- s B$ X1 A: g1 x2 G6 M' A- A
- }* U" ^: g$ S5 [
- echo "All Done";
% `; _4 P* x+ d8 \
复制代码 功能:支持提取一行多个email地址,结果全部保存到result.txt ,没有做去重
* S- z4 q0 t9 E" e& p5 _1 L+ D使用:代码里面的源文件是x.txt ,可以换成你自己的文件名,php保存为getMail.php 然后 php getMail.php 结果保存在result.txt里面 |
评分
-
查看全部评分
|