Hey Guys,
I have a simple one for you, if you could help me out with this, I would be VERY grateful because its driving me nuts!

So I am trying to search a file for a word. Thats it, not trying to write to the file, just read and search. I have simplified the code as much as possible and it still cant find it. Here is the code:

<?
$file = file_get_contents("users/crimsonlung.txt");
if(!strpos($file, "word")) {
echo "string not found!";
}
?>

So this should be looking into a file called "crimsonlung.txt" in the users folder for the text "word."

in the file, I have nothing but the text: "word" in it. So what is the problem?

I've echoed the variables just to make sure it was searching in the right spot and finding the data, and it was. So why is "string not found" being echod' each time? What could possibly be wrong?

Is there any other code I could use?

Thanks for any help!

    Hi crimson,

    If the only text in the file is 'word' then the result of strpos() will be '0'. But, then ...

    !strpos($file, "word") -> TRUE

    ... since !0 = TRUE.

    What you want to do is:

    if(strpos($file, "word") === FALSE) {
    echo "string not found!";
    }
    

    Notice the three equality signs! See the manual for details.

      Write a Reply...