|
发表于 2017-12-4 00:39:19
|
显示全部楼层
本帖最后由 bjzhush 于 2017-12-4 00:45 编辑 1 a; F, f% [( w# W' S- r! W
0 U0 m: g3 p7 Y$ g) z4 B花了几分钟帮你写了下
! @, W% s+ ~: d/ x0 e例如源文件为这个txt
' V D" y! a, J' ^, A➜ ~ cat x.txt' ]4 d2 Z! y D; S# q% s. G: H
hitest- a1 {$ b8 V/ E( K
[email protected]
: H$ w6 A' j2 m: @hi [email protected] test! ?) y( S8 J) L" m$ t) b
[email protected] [email protected]
& s% K$ G/ ^ a" C0 ]; |2 K8 \执行后,得到result.txt" N) [7 J% ]3 A- Q0 C
➜ ~ cat result.txt 9 q6 {8 d% `% e' j
[email protected]
! y) {( G, v4 S \7 @( l[email protected]
. h; O s* }; _5 l[email protected]
3 p7 s8 m/ D2 t# C[email protected], l0 M$ C" `* `$ U. k+ N
% U2 Q* T o. l4 s
代码如下:" ~ o7 r8 t* A( p
- <?php6 p, r% H7 I# G( O9 C* y
- $file = 'x.txt';
. r% `+ R, |9 s9 Q5 Z5 h' A$ [ - $fp = fopen($file, 'r');, x& a% d7 k- t/ M% U0 }; R
- if (!$fp) {
! N- c9 z# `8 E# N% ?& @2 ^ - exit('failed to open'.$file);5 J( L% G9 s. u9 @7 ]( x! M
- }
4 ?' j H1 c5 ]! n- {( Y) f - $lineNumber = 0;
3 s C8 `0 K4 ~; l7 ~ - while($line = fgets($fp)) {
+ e9 J6 X' l7 v& j+ ?/ U - $lineNumber++;" |9 m4 L, E2 q! F: |: H8 n
- $pattern = '/[a-z0-9_\-\+]+@[a-z0-9\-]+\.([a-z]{2,4})(?:\.[a-z]{2})?/i';( m% |/ x0 t, N ~1 t: f3 j$ U9 k& B
- preg_match_all($pattern, $line, $matches);
% S$ ` N+ G, i' T7 _ - if (!empty($matches[0])) {
0 {0 g1 v. @0 j6 H - foreach ($matches[0] as $mail) {; A% l! z- ^. W, D3 M) Q
- echo $mail.' found from line '.$lineNumber.PHP_EOL;; ]- ?$ c$ Z' t/ F) |& r
- file_put_contents('result.txt', $mail.PHP_EOL, FILE_APPEND);
# L+ \( Q3 z: @! {- F9 c @1 j - }3 M, p9 X. b8 V+ n) B3 J
- }' _9 K1 J z1 m; r/ U/ e( H& V
- }. U! x- e' C' x0 \- n
- echo "All Done";0 r b& z0 C0 Y% ]! p) C
复制代码 功能:支持提取一行多个email地址,结果全部保存到result.txt ,没有做去重' D' J( R7 e* Y) D/ d
使用:代码里面的源文件是x.txt ,可以换成你自己的文件名,php保存为getMail.php 然后 php getMail.php 结果保存在result.txt里面 |
评分
-
查看全部评分
|