Hello so I have a text file that as this
data.txt
data e.g. username
I need this to out put into a $tag
File.php
if ($username == "$data") { echo "User Found" } else{ echo "User not found";}
Any help on this would really be nice :)?
Hello so I have a text file that as this
data.txt
data e.g. username
I need this to out put into a $tag
File.php
if ($username == "$data") { echo "User Found" } else{ echo "User not found";}
Any help on this would really be nice :)?
One method that can be used here and that I took from one of my script libraries, is making use of preg_match()
and using both the \b
word boundary option, and the i
switch for case-insenstivity, which will work for single or multi-line data. Will match "john" or "John", as an example.
<?php
$_POST['name'] = "john";
$var = trim($_POST['name']); // should there be a space entered
// $var = $_POST['name'];
$pattern = "/\b$var\b/i";
$fh = fopen('data.txt', 'r') or die("Can't open file");
while (!feof($fh)) {
$line = fgets($fh, 4096);
if (preg_match($pattern, $line)) {
echo "MATCH FOUND";
}
else{
echo "NOT FOUND";
}
}
fclose($fh);
However, this will not work if an entry is seperated by a space. I.e.: "john doe".
You will need to use the following, if that is the case and stripos()
for case-insensitivity.
<?php
$_POST['name'] = "john doe";
$search = trim($_POST['name']);
// $search = $_POST['name'];
// Read from file
$lines = file('data.txt');
foreach($lines as $line)
{
// Check if the line contains the string we're looking for, and print if it does
if(stripos($line, $search) !== false){
echo "Found: " . $line; }
}
References:
Footnotes:
$data = file_get_contents('filehere');
if($username == $data) { echo "User found"; } else { echo "User not found"; }
user_pass.txt:
thomas password
john password2
Code:
<?PHP
$data = fopen('user_pass.txt', 'r');
while (($line = fgets($data)) !== FALSE) {
$data = explode(' ', $data);
if ($username == $data[0] && $password == $data[1]) {
echo "User found";
} else {
echo "User not found";
}
}
?>
Unless I missed something? If you get an errors please reply.
Function search: post function search in PHP
function search($search, $string) {
$pos = strpos($string, $search);
if ($pos === false) {
echo "The string '$search' was not found.";
} else {
echo "The string '$search' was found ";
echo "and exists at position $pos.";
}
}
love.txt as file
we
love
php
programming
open file:
$file = fopen("love.txt", "r");
call function search and function fread for open file 'love.txt'
search("love", fread($file, filesize("love.txt")));
Result:
The string 'love' was found and exists at position 4.