ds0678
2010-12-29 12:38
浏览 81
已采纳

如何分段阅读此文本文件?

hi I need to read a text file and then explode it to two piece then match it with give name then dispay if it matches...so far the code I made is given below..but it doesnt work..can any one tell me what wrong is with this code??

$name = "thomas";
$filename = file("land.txt");
//$contents = fread($handle, filesize($filename));
for($i=0; $i<count($filename); $i++)
        {
            $string = explode(":", $filename[$i]);

            if($name == $string[1])
                $id = $string[0];
        }

echo $id;

this case it should display "D1"; but it doesnt!!

content of "land.txt" file

D1:thomas
D6:benny
D7:alwyn
D25:mathew
D9:peter
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • doubi1910 2010-12-29 12:42
    已采纳

    Try

    $filename = file("land.txt", FILE_IGNORE_NEW_LINES);
    

    Or

    $string = explode(":", trim($filename[$i]));
    

    What is FILE_IGNORE_NEW_LINES

    点赞 评论
  • douyakao5308 2010-12-29 12:54

    Over here

    if($name == $string[1]) use if($name == trim($string[1])) .

    Try this and let me know.

    点赞 评论
  • duandi5328 2010-12-29 13:33
      $name = 'thomas';
      $content = file('land.txt');
      $id = NULL;
      foreach($content as $no => $line){
       $array = explode(':', $line);
       if($array[1] == $name)
         $id = $array[0];
      }
      echo $id;
    点赞 评论

相关推荐 更多相似问题