|
发表于 2017-12-4 00:39:19
|
显示全部楼层
本帖最后由 bjzhush 于 2017-12-4 00:45 编辑
6 t( @+ s7 v: B( Y. p+ r \6 F1 `+ b) H: x1 }
花了几分钟帮你写了下 / l$ v; H6 w' r9 r+ Z/ w
例如源文件为这个txt
1 a6 U+ a1 l, o( a- ^➜ ~ cat x.txt
5 F) @/ P5 x2 S! |hitest
" K/ \$ ~% p- J3 T, g! e[email protected]/ f; z( n! k( w4 K( l V
hi [email protected] test
# l) Y) A9 d/ N1 Q[email protected] [email protected]
, B8 |- C% C6 \. c- {执行后,得到result.txt$ V9 P: V( M2 B# }4 B9 [
➜ ~ cat result.txt
* W# m5 E6 {; w: l1 b5 J2 W( C- s9 V[email protected]- `: U p$ j( E# O+ @
[email protected]
& ^2 u7 g) }) S[email protected]
L- E) m& }' A& b# V- k! ^# D! i[email protected]
& W0 E$ `! i3 Z7 C7 Z% _3 i' ]9 E2 t* p
代码如下:
, w' _% a5 a+ J, N* _- <?php& r5 C8 n" K+ C- C8 L1 `; C
- $file = 'x.txt';' d# S/ h# F1 z5 U4 v8 a2 J
- $fp = fopen($file, 'r');( r* l# }, H9 \" {
- if (!$fp) {
% s+ @7 q! s- y, L - exit('failed to open'.$file);
# U& h7 d9 H2 c f5 v - }
" k! c$ Q. p v" h- Y5 z% l: t - $lineNumber = 0;1 Y- j2 ?* Y& M5 Z( W% D5 y
- while($line = fgets($fp)) {" E+ O; [3 E5 D# j
- $lineNumber++;% l$ H" J; J* w7 f2 v- {3 a4 R; Q
- $pattern = '/[a-z0-9_\-\+]+@[a-z0-9\-]+\.([a-z]{2,4})(?:\.[a-z]{2})?/i';
- D* g( R7 f7 [/ `# s0 V- q - preg_match_all($pattern, $line, $matches);
3 h2 j8 s4 a- \. T! l$ z. c5 S) y6 I - if (!empty($matches[0])) {& N5 l/ U1 [1 H; k4 {, q, B
- foreach ($matches[0] as $mail) {5 `1 i- p' o& p$ n: {
- echo $mail.' found from line '.$lineNumber.PHP_EOL;0 l; h$ J- L- a9 R$ ^
- file_put_contents('result.txt', $mail.PHP_EOL, FILE_APPEND);! I" b' }/ B& C/ ~& i
- }
' e0 ?* I1 k5 r) v - }. W$ ^. a; | V% Z' {
- }
" z7 Z. _3 {& E! u1 T - echo "All Done";2 g; T: J' B" m5 d+ X+ z
复制代码 功能:支持提取一行多个email地址,结果全部保存到result.txt ,没有做去重' ?8 `5 m h" d9 m+ Q5 `$ u
使用:代码里面的源文件是x.txt ,可以换成你自己的文件名,php保存为getMail.php 然后 php getMail.php 结果保存在result.txt里面 |
评分
-
查看全部评分
|