RE: [PHP] file() and array values contain extra \n
| From: | Dave [Hawk-Systems] | Date: | Tue, 16 Jul 2002 14:35:01 +0000 |
| Subject: | RE: [PHP] file() and array values contain extra \n | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-107878@lists.php.net to get a copy of this message | ||
>On Tuesday 16 July 2002 21:53, Dave [Hawk-Systems] wrote:
>> $users=file('.users');
>> # puts users in the file into an array so we can
>> # check for valid or priv users with
>> if(in_array($HTTP_SERVER_VARS["REMOTE_USER"], $users)){}
>>
>> # we add additional users to the .users file with the following
>> $users[]=$newuser;
>> # adds the new user to the end of the above created users array
>> # then write the array to the file
>> $fd = fopen (".users", "w+");
>> fwrite ($fd, join("\n",$users));
>> fclose ($fd);
>>
>> the problem is after adding users, only the last user returns the user name
>> in the array, all the other users have an additional "/n" at the end of
>> them, which causes the check to barf.
>
>Well, if you RTFM, you'll know that file() "returns the file in an array ...
>with the newline still attached". So one possible solution is after using
>file(), loop through $users and remove the trailing "\n".
thanks, I am/was looking to avoid looping through the array unnecessarily, and
simply have them removed at the time of dumping the file into the array.
Dave