Jump to content

Can't check in_array from file?


Rifts

Recommended Posts

Hey guys,

 

I have this super simple code which will not work and I'm very confused as to why.

 

Here is the code

 

// read the file into an array called $users
$users= file('users.txt');

if (in_array("joe", $users)) {
    echo "Got Joe";
}

 

When I print_r($users) everyone looks fine like this but it never returns true, Why?

 

Array
(
    [0] => joe

    [1] => bob

    [2] => mike

    [3] => ted

    [4] => fred
)

Link to comment
Share on other sites

Something a little extra.

 

<?php
$username = 'joe';
$username = ucwords(strtolower(trim($username)));
$filename = 'users.txt';
if(file_exists($filename)){
$users= array_map('trim',file($filename));
if(in_array(strtolower($username), array_map('strtolower', $users))){
    echo "Got $username";
} else {
    echo "$username not found";
}
} else {
    echo "$filename doesn't exist.";
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.