首页 > 解决方案 > 如何从 txt 文件中用 PHP 找到一个特定的词?

问题描述

我有一个 PHP 脚本可以从 txt 文件中找到一个关键字,但结果显示了一整行。在这种情况下,我希望结果只显示特定的单词。

这是 txt 源文件:

Lorem ipsum dolor sit amet aaaaa@xxx.com, consectetur adipiscing bbbbb@xxx.com elit, sed do eiusmod tempor incididunt ut 

labore et dolore magna aliqua cccc@xxx.com. 

Ut enim ad minim veniam ddd@xxx.com, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea jjjj@xxx.com commodo 

consequat. 

Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint 

occaecat cupidatat non proident@xxx.com, sunt in culpa qui officia deserunt mollit anim@xxx.com id est laborum.

我使用这个 PHP 代码:

<?php
$file = 'D:\tes.txt';
$searchfor = 'xxx.com';

// the following line prevents the browser from parsing this as HTML.
header('Content-Type: text/plain');

// get the file contents, assuming the file to be readable (and exist)
$contents = file_get_contents($file);
// escape special characters in the query
$pattern = preg_quote($searchfor, '/');
// finalise the regular expression, matching the whole line
$pattern = "/^.*$pattern.*\$/m";
// search, and store all matching occurences in $matches
if(preg_match_all($pattern, $contents, $matches)){
   echo "Found matches:\n";
   echo implode("\n", $matches[0]);
}
else{
   echo "No matches found";
}

?>

使用该代码,结果是:

Found matches:
Lorem ipsum dolor sit amet aaaaa@xxx.com, consectetur adipiscing bbbbb@xxx.com elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua cccc@xxx.com. 
Ut enim ad minim veniam ddd@xxx.com, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea jjjj@xxx.com commodo consequat. 
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident@xxx.com, sunt in culpa qui officia deserunt mollit anim@xxx.com id est laborum.

但我想要这样:

aaaaa@xxx.com
bbbbb@xxx.com
ddd@xxx.com
cccc@xxx.com
jjjj@xxx.com
.........
.....

需要一些帮助,因为我不知道编码,但我需要这个脚本..谢谢

标签: phpsearchtext

解决方案


您匹配整行,因为您正在使用.*和锚点^$断言行的开头和结尾。要匹配您可以更新为的所有电子邮件地址$pattern

$pattern = "/\S+@xxx\.com\b/m";

您的代码可能如下所示:

$file = 'D:\tes.txt';
// the following line prevents the browser from parsing this as HTML.
header('Content-Type: text/plain');

// get the file contents, assuming the file to be readable (and exist)
$contents = file_get_contents($file);
$pattern = "/\S+@xxx\.com/m";
// search, and store all matching occurences in $matches
if(preg_match_all($pattern, $contents, $matches)){
    echo "Found matches:\n";
    echo implode("\n", $matches[0]);
}
else{
    echo "No matches found";
}

演示

这将不匹配空白字符一次或多次S+,并在后面加上一个单词边界并在结尾处@签名xxx.com \b


推荐阅读