Thank you for taking the time to read this and I will appreciate every single response no mater the quality of content. :)
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.
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'];
The text file below is called "begin.txt":
"AULLAH1" "01/07/2010 15:28 " "55621454" "123456" "123456.00"
"USERNAME" "7/3/2010 6:21 PM" "55621454" "123456" "123456.00"
"AULLAH1" "07/07/2010 15:05 " "55621454" "189450" "123456.00"
"SUPREMEGAMER" "7/8/2010 6:42 PM" "55621454" "123456" "123456.00"
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. :)
Thank you.
You can use SplFileObject and iterate over the file line by line.
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;
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;
If you want to make sure the $username was found at position 1, change the if condition to test for === 1.
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;
}
}
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;
}
The above would output
"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);
and to look at the last item
echo end($lines);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With