【问题标题】:How to found a spesific word with PHP from txt file?如何使用 PHP 从文本文件中查找特定单词?
【发布时间】:2018-07-22 16:50:49
【问题描述】:

我有一个 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
.........
.....

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

【问题讨论】:

    标签: php search text


    【解决方案1】:

    您匹配整行,因为您使用.* 和锚点^$ 来断言行的开头和结尾。要匹配所有电子邮件地址,您可以将 $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";
    }
    

    Demo

    这将不匹配空格字符一次或多次S+,并且@ 符号后跟xxx.com,最后是word boundary \b

    【讨论】:

    • 谢谢它的工作.. 如果我想使用某种关键字,如“xxx.com、+3523 和 +1304”,我应该添加什么?谢谢你..
    • 这取决于您期望匹配的内容。如果要查找单词,可以使用单词边界\b,但如果要查找特定模式,则必须定义该模式。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-31
    • 1970-01-01
    • 2013-08-05
    • 1970-01-01
    • 2011-12-17
    • 2021-10-19
    • 2021-02-01
    相关资源
    最近更新 更多