|
发表于 2017-12-4 00:39:19
|
显示全部楼层
本帖最后由 bjzhush 于 2017-12-4 00:45 编辑 1 t- G. F5 K' L z
' U( B+ ~! D0 r- ~& [5 @花了几分钟帮你写了下
. t& T) u1 B! q- k" \" R例如源文件为这个txt7 N% F& Y# R. }7 y
➜ ~ cat x.txt4 U' Z- ^' J" l5 z* o
hitest
# @1 T6 N2 c3 }5 ][email protected]
9 ?( C- l! {' A9 ?1 Yhi [email protected] test
0 R! D5 t" o7 P) Z[email protected] [email protected]
# E3 J; I0 h, s6 O# F) T4 f/ G) e执行后,得到result.txt
% R9 |" m* w8 Z1 C➜ ~ cat result.txt
$ S6 t; B% I- |3 R8 X# m3 G* i$ |[email protected]
[0 t; b; l. E[email protected]
% p1 T1 \7 M' R( T4 w( Q[email protected]
, g0 x% U2 D! b% Q- T3 e/ s[email protected]
: ?* u. x# F" {: l4 `' @; B
v2 m6 w- l6 ]代码如下:
# ^6 ]( x$ m8 W$ C# j8 E1 Y- <?php7 H" f+ ]$ g5 L1 X" O7 w
- $file = 'x.txt';
$ L* s; u- h1 `: ] - $fp = fopen($file, 'r');2 A2 Q* x7 h; a h# D- V
- if (!$fp) {7 R2 T- k2 a9 c. U! }1 x
- exit('failed to open'.$file);8 k, q" E% u. `/ O% d Y! x
- }- A" V3 S! R! l' \
- $lineNumber = 0;
2 ^; K7 t- W \+ O& d! S# [3 C - while($line = fgets($fp)) {
" D+ Z u: A1 s- W! {/ o - $lineNumber++;( P' w9 o3 k2 W' B' ?6 U, @) {2 Q
- $pattern = '/[a-z0-9_\-\+]+@[a-z0-9\-]+\.([a-z]{2,4})(?:\.[a-z]{2})?/i';9 X" }/ T9 {% I3 D# ?: c3 G
- preg_match_all($pattern, $line, $matches);
4 E1 g3 u- n; N. |" r - if (!empty($matches[0])) {( _1 v4 q" r5 v, a3 E
- foreach ($matches[0] as $mail) {
9 O( E& F+ S& H2 I - echo $mail.' found from line '.$lineNumber.PHP_EOL;/ I V% Y- z) ^# i- h
- file_put_contents('result.txt', $mail.PHP_EOL, FILE_APPEND);" s+ @5 x+ G$ e* e+ W% ]
- }
6 J) T! c- ?+ ^2 @9 D5 B" s+ `* Y - }
^/ l& }: w# p% S1 X0 w - }4 I3 k8 X+ C V7 ?
- echo "All Done";
5 O& Z0 g) {7 n5 h7 _4 C
复制代码 功能:支持提取一行多个email地址,结果全部保存到result.txt ,没有做去重
1 v# X7 ~( Y2 o8 S使用:代码里面的源文件是x.txt ,可以换成你自己的文件名,php保存为getMail.php 然后 php getMail.php 结果保存在result.txt里面 |
评分
-
查看全部评分
|