|
发表于 2017-12-4 00:39:19
|
显示全部楼层
本帖最后由 bjzhush 于 2017-12-4 00:45 编辑 , ^/ a. s/ f: a* f) E6 t+ w9 z
& o: L5 c# H6 A
花了几分钟帮你写了下
( E& G2 ?) \7 U" @/ P; y例如源文件为这个txt
8 Q; t7 f0 l+ ?3 J; Y➜ ~ cat x.txt
, S% x: F' O; B2 g' Thitest: h) l9 _. T4 n# S+ E R' y5 c' Y
[email protected]
5 A$ h p' c. @7 xhi [email protected] test
% V4 I8 f1 Y2 y( r! S- ^[email protected] [email protected]+ `6 p* j( N" T @' D; w8 ?1 I
执行后,得到result.txt$ m: \. W" s P
➜ ~ cat result.txt
5 L! k: D0 X2 m[email protected]
7 i0 I; @7 f% A* k9 C" y[email protected]
/ `) E, r: y1 u9 ~: G* Q[email protected]
. F( A: _ i @[email protected]
! A3 F, B8 ^$ G6 A3 i1 Y
3 T' s9 l/ R2 v代码如下:7 R, a* W0 F( P q2 t V. o4 `$ A5 Y
- <?php( m* w. e5 |* ]6 x
- $file = 'x.txt';6 `# l& e8 R" N. P1 Q# L0 g
- $fp = fopen($file, 'r');% l5 T1 b* P& `$ c
- if (!$fp) {
, z' \9 ^% d9 t4 L - exit('failed to open'.$file);
4 l" H! @ O |9 C - }4 _/ K/ a/ d& ~+ z
- $lineNumber = 0;" e* G4 p P& P; K5 Q
- while($line = fgets($fp)) {; E' a! Y+ q O; [
- $lineNumber++;
! N$ n6 j! Z4 N9 C: |+ C - $pattern = '/[a-z0-9_\-\+]+@[a-z0-9\-]+\.([a-z]{2,4})(?:\.[a-z]{2})?/i';, g6 [9 ]2 a8 ^6 {# l# y8 z
- preg_match_all($pattern, $line, $matches);; M) _1 n( z: \3 I
- if (!empty($matches[0])) {4 M1 L2 \. Q7 U
- foreach ($matches[0] as $mail) {$ f5 C$ B0 ?8 y# X2 A2 M( _# T, n
- echo $mail.' found from line '.$lineNumber.PHP_EOL;- q( V7 ^$ \; d% y3 t- Q, Y+ H9 Z3 |
- file_put_contents('result.txt', $mail.PHP_EOL, FILE_APPEND); W. h1 Q; u B& c M2 K' |
- }3 s) t9 s! {( j" G! i" F3 {' w7 X
- }
6 O$ P" f3 h9 B6 ~- L! ~ - }# y7 V- i3 |5 A: y1 k
- echo "All Done";9 P0 i4 ? @7 {# ]7 a
复制代码 功能:支持提取一行多个email地址,结果全部保存到result.txt ,没有做去重% C6 w4 {4 ^) H s: z9 j6 z
使用:代码里面的源文件是x.txt ,可以换成你自己的文件名,php保存为getMail.php 然后 php getMail.php 结果保存在result.txt里面 |
评分
-
查看全部评分
|