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.

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

报告相同问题?

悬赏问题

  • ¥15 is not in the mmseg::model registry。报错,模型注册表找不到自定义模块。
  • ¥15 安装quartus II18.1时弹出此error,怎么解决?
  • ¥15 keil官网下载psn序列号在哪
  • ¥15 想用adb命令做一个通话软件,播放录音
  • ¥30 Pytorch深度学习服务器跑不通问题解决?
  • ¥15 部分客户订单定位有误的问题
  • ¥15 如何在maya程序中利用python编写领子和褶裥的模型的方法
  • ¥15 Bug traq 数据包 大概什么价
  • ¥15 在anaconda上pytorch和paddle paddle下载报错
  • ¥25 自动填写QQ腾讯文档收集表