-1

I've just started php and I can't find an easy way to compare to a variable each first word of a line of a text file in a format("userid firstname lastname password").

Ryan
  • 29
  • 1
  • 8
  • What would you consider "easy"? You open the file, you read line by line and extract the first word. Which of these steps do you have a problem with? – El_Vanja Nov 29 '20 at 23:16
  • I'm having issues with reading a file line by line and extracting the first word. So everything except opening the file. – Ryan Nov 29 '20 at 23:25
  • 1
    Does [this](https://stackoverflow.com/questions/13246597/how-to-read-a-large-file-line-by-line) help with the first part? And [this](https://stackoverflow.com/questions/2476789/how-to-get-the-first-word-of-a-sentence-in-php) with the second? – El_Vanja Nov 29 '20 at 23:30

1 Answers1

0
<?php
     $mf = fopen("users.txt", "r") or die("Unable to open user database file!");
     $fc = file_get_contents('users.txt');
     $cv = "userid";
     if($fc == $cv) {
          //the contents of the file contain userid
     }
     fclose($mf);
?>

This isn't exactly what you wanted, but it should steer you in the right direction. You will have to look into parsing the first word on each line and comparing it to a variable. Replace the if statement with the parsing.