gsmith Posted February 26, 2006 Share Posted February 26, 2006 I am currently reading a file into an array: $newthing=file(filename); each line I read in will be of the general format:$uname[0]=username; $known_as[0]=realname;$uname[1]=username; known_as[1]=realname;etc.I have another variable, we'll call it $x. $x will equal the username portion of one of the above lines. I want to do other things (that I do know how to do) ONLY IF $x is NOT in the line that I've read into the $newthing array. How can I search and determine this line by line? Link to comment Share on other sites More sharing options...
sbrownii Posted February 26, 2006 Share Posted February 26, 2006 Each line (element in the array) will be a string.Use some string functions. Ever heard of php.net?Here is one function for finding a string in a string -> http://us2.php.net/manual/en/function.stristr.phpMake note of the list on the left - those are other string functions. Have a look.... Link to comment Share on other sites More sharing options...
gsmith Posted February 26, 2006 Author Share Posted February 26, 2006 Before your response, I had searched PHP.net.I tried using strpos() foreach($temp as $value) { if(!(strpos($value, $usr))) { fwrite($fs, $value); } } $temp is the array I got using the file() command. I want to write to the file $fs all the lines in $temp, except for the line that contains within it somewhere the same exact value that $usr holds.The above code has worked if the line in question is the last line of the file. But if it's in the middle, this code stops writing the remaining lines back to the file once the one line in question is found. Link to comment Share on other sites More sharing options...
hacknsack Posted February 26, 2006 Share Posted February 26, 2006 Try generating a string and then write the string to the file: $output = '';foreach($temp as $value) { if(!(strpos($value, $usr))) { $output .= "$value\n"; } }fwrite($fs, $output); [edit] fixed the typos[/edit]-hs Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now