Match $var in text file ?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Peuplarchie
Forum Contributor
Posts: 148
Joined: Sat Feb 04, 2006 10:49 pm

Match $var in text file ?

Post by Peuplarchie »

Good day to you all,
stupid as it sound, all the thing that I have tried to match a variable into a text file and execute according to it result.

Code: Select all

 
 
$active = "user_list.txt";
$pos = strpos($active, $_SESSION['username']);
if ($pos === false) {
echo "<b id=\"userinfo\"><img src=\"enligne.png\" valign=\"bottom\" width=\"13px\"/></b>";
} else {
echo "<b id=\"userinfo\"><img src=\"horsligne.png\" valign=\"bottom\" width=\"13px\"/></b>";
} 
 
 
 


Thanks !
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: Match $var in text file ?

Post by Mark Baker »

You're using strpos to search in the file name, not in the file itself.
You need to read the file content into a string variable, then search in that.

Code: Select all

 
$active = "user_list.txt";
$fileData = file_get_contents($active);
$pos = strpos($fileData, $_SESSION['username']);
if ($pos === false) {
echo "<b id=\"userinfo\"><img src=\"enligne.png\" valign=\"bottom\" width=\"13px\"/></b>";
} else {
echo "<b id=\"userinfo\"><img src=\"horsligne.png\" valign=\"bottom\" width=\"13px\"/></b>";
} 
 
Note that if you have a user called "Adam", and another called "Adam Smith", then searching for "Adam" in this way might return true because it matches "Adam Smith"
Post Reply