EDIT: The main goal I wanted to ask this question for is:
IGNORE IF YOU WANT THE ANSWER, THE ANSWER IS BELOW:
I'm developing an AdminCP for my project, and if the administrator typed "User1" in a HTML form in the AdminCP it must return "This user is suspended!" if the line in the txt file was "User1:suspended:24/01/2012" .
While the format of users in the txt file is:
USERNAME:STATUS:REGISTRATION DATE
User1:suspended:24/01/2012
(All the texts and strings are just examples).
So in the below answer ... $searchfor = 'Field2';
must be $searchfor = $_POST['username'];
for example.
And I wanted to use:
if( $data[1]=="suspended" ) {
echo "The user which owns ".$data[0]." is suspended!";
}
While $data[0] is the username (User1), and $data[1] is the status (suspended).
Thanks to @MagnusEriksson and PHP to search within txt file and echo the whole line . I just discovered the solution by myself:
<?php
$file = 'test.txt';
$searchfor = 'Field2';
// 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
preg_match_all($pattern, $contents, $matches);
$wholeLine = implode("\n", $matches[0]);
$data = explode(":", $wholeLine);
echo $data[0]; // Field1
echo $data[1]; // Field2
?>