PHP脚本可抓取整行 [英] PHP script to grab entire line

查看:56
本文介绍了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表单的输入字段的值为"username",在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/01/07 15:28""55621454""123456""123456.00"

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

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

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

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

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

"SUPREMEGAMER""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""07/07/2010 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. :)

谢谢.

推荐答案

您可以使用

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);

这篇关于PHP脚本可抓取整行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆