Why the construction does not work:
<?php $user = 'user2'; $lines = file('users.txt'); foreach($lines as $line) { if ( $line == $user ) { echo $line; break; } } ?> Структура users.txt: user1 user2 user3 Why the construction does not work:
<?php $user = 'user2'; $lines = file('users.txt'); foreach($lines as $line) { if ( $line == $user ) { echo $line; break; } } ?> Структура users.txt: user1 user2 user3 Why the construction does not work
Most likely because file('users.txt') saves in the $lines variable an array of lines with the ending \r\n (or \n ) . Flags will help you: FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES :
$user = 'user2'; $lines = file('users.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); foreach ($lines as $line) { if ( $line == $user ) { echo $line; break; } } Source: https://ru.stackoverflow.com/questions/937135/
All Articles