PHP脚本抓取整行

2022-01-20 00:00:00 line text-files fetch php

感谢您抽出宝贵时间阅读本文,无论内容质量如何,我都会感谢您的每一个回复.:)

Thank you for taking the time to read this and I will appreciate every single response no mater the quality of content. :)

我正在尝试创建一个在文本文件中搜索特定文本的 php 脚本.一个人在 HTML 表单中键入特定文本,php 脚本应该在文本文件中搜索该特定文本.

I'm trying to create a php script which searches a text file for specific text. A person types in the specific text within a HTML form and the php script should search for that specific text within the text file.

HTML 表单的输入字段的值为用户名";在 php 文档中,变量$username"是输入的数据,示例如下:

The value of the input field of the HTML form is "username" and within the php document, the variable "$username" is the entered data, example shown below:

$username = $_POST['username'];

下面的文本文件名为begin.txt":

The text file below is called "begin.txt":

AULLAH1"2010 年 1 月 7 日 15:28"55621454"123456"123456.00"

"AULLAH1" "01/07/2010 15:28 " "55621454" "123456" "123456.00"

用户名"2010 年 7 月 3 日下午 6:21"55621454"123456"123456.00"

"USERNAME" "7/3/2010 6:21 PM" "55621454" "123456" "123456.00"

AULLAH1"2010 年 7 月 7 日 15:05"55621454"189450"123456.00"

"AULLAH1" "07/07/2010 15:05 " "55621454" "189450" "123456.00"

超级玩家"2010 年 7 月 8 日下午 6:42"55621454"123456"123456.00"

"SUPREMEGAMER" "7/8/2010 6:42 PM" "55621454" "123456" "123456.00"

如果有人输入 HTML 表单AULLAH1",我希望 php 脚本抓取最后一个AULLAH1".文本文件中的条目(例如,它应该抓取第三行而不是第一行,因为第三行是包含文本AULLAH1"的最后一个条目).此外,当一个人输入特定文本时,它不应该简单地抓取文档或文本,它应该抓取整行:AULLAH1".2010 年 7 月 7 日 15:05"55621454"189450"123456.00"并将其放入一个 php 变量中,可能类似于$grabbed"?如果可能的话.

If a person types into the HTML form "AULLAH1", I'd like the php script to grab the last "AULLAH1" entry in the text file (e.g. It should grab the third line and not the first, as the third line is the last entry to contain the text "AULLAH1"). Also with that, when a person types in a specific text, it shouldn't simply grab the document or the text, it should grab the whole line: "AULLAH1" "07/07/2010 15:05 " "55621454" "189450" "123456.00" and place it into a php variable, maybe something like "$grabbed"? If possible at all.

感谢所有帮助,我期待您的回复;谢谢.:) 如果我没有清楚地解释任何事情和/或您希望我更详细地解释,请回复.:)

All assistance is appreciated and I look forward to your replies; thank you. :) If I didn't explain anything clearly and/or you'd like me to explain in more detail, please reply. :)

谢谢.

推荐答案

你可以使用 SplFileObject 并逐行遍历文件.

You can use SplFileObject and iterate over the file line by line.

请注意,遍历文件比使用 file()file_get_contents() 更节省内存,因为它不会将整个文件内容读入数组或变量.

Please note that iterating over the file is much more memory efficient than using file() or file_get_contents() because it does not read the entire file content into an array or a variable.

$username = 'AULLAH1';
$file = new SplFileObject("data.csv");
$grabbed = FALSE;
while (!$file->eof()) {
     $data = $file->fgetcsv(' ');
     if($data[0] === $username) {
         $grabbed = $file->current();
     }
}
echo $grabbed;

因为 fgetcsv() 在解析行时考虑了分隔符、包围和转义字符,所以它并不是很快.如果您必须以这种方式解析数百或数千行,请确保您确实需要它.

Because fgetcsv() takes into account delimiters, enclosures and escape characters when parsing the line, it is not exactly fast though. If you have to parse a couple hundred or thousand lines this way, make sure you actually have the need for it.

另一种方法是检查当前行是否在某处包含用户名字符串.在您在问题中显示的文件格式中,这是可行的,因为其余字段包含数字,因此不会有任何误报:

An alternative would be to just check if the current line contains the username string somewhere. In the file format you show in the question, this would be feasible because the remaining fields contain digits, so there cannot be any false positives:

$username = 'AULLAH1';
$file = new SplFileObject("data.txt");
$grabbed = FALSE;
foreach($file as $line) {
    if(strpos($line, $username) !== FALSE) {
        $grabbed = $line;
    }
}
echo $grabbed;

如果您想确保在位置 1 找到 $username,请更改 if 条件以测试 === 1.

If you want to make sure the $username was found at position 1, change the if condition to test for === 1.

如果出于某种原因,您希望所有出现用户名的行都有,您可以编写一个自定义 FilterIterator 来迭代文件内容,例如

If, for some reason, you want to have all lines where the username occurs, you can write a custom FilterIterator to iterate over the file contents, e.g.

class UsernameFilter extends FilterIterator
{
    protected $_username;
    public function __construct(Iterator $iterator, $username)
    {
        $this->_username = $username;
        parent::__construct($iterator);
    }
    public function accept()
    {
        return strpos($this->current(), $this->_username) !== FALSE;
    }
}

然后你可以简单地使用foreach.FilterIterator 会将每一行传递给 accept() 并且只有那些它返回 TRUE 的行才会被实际使用.

Then you can simply use foreach. The FilterIterator will pass each line to accept() and only those lines for which it returns TRUE are actually used.

$filteredLines = new UsernameFilter(new SplFileObject('data.txt'), 'AULLAH1');
foreach($filteredLines as $line) {
    echo $line;
}

上面会输出

"AULLAH1" "01/07/2010 15:28 " "55621454" "123456" "123456.00"
"AULLAH1" "07/07/2010 15:05 " "55621454" "189450" "123456.00"

如果你想把这些行放在一个数组中,你可以这样做

If you want these lines in an array, you can do

$lines = iterator_to_array($filteredLines);

并查看最后一项

echo end($lines);

相关文章