duanjue7745 2012-04-19 19:55
浏览 39
已采纳

in_array函数没有给出预期的结果

I am using in_array to search in an array if a value exists. My code is below:

$file = 'domains.txt'; //reading txt file
$lines = file($file);//file in to an array

if (in_array("yahoo.com", $lines)) {
    echo "Got Domain";
}

However, its not working, it only displays a blank page, but yahoo.com is available in domains.txt so it should return true, but i am stuck with it, please help, thanks.

  • 写回答

1条回答 默认 最新

  • dongmale0656 2012-04-19 19:56
    关注

    Add the FILE_IGNORE_NEW_LINES flag when calling file():

    $lines = file($file, FILE_IGNORE_NEW_LINES);
    

    When the line with yahoo.com is being read, it is being stored as:

    "yahoo.com
    "
    

    The above flag will tells file() not to append the newline ( ) character to each string in the returned array.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?