2015-07-21 34 views
-1

下面的代碼正在工作,但!當示例文本文件在其他版本的文本編輯器中編輯.. 它不會!什麼是最好的方式來閱讀一個文本文件避免在PHP文本編輯器問題?

什麼是最好的方式來逐行閱讀文本沒有文本編輯器問題使用php?

示例文本文件 名 - 約翰(行0) 年齡 - 24(線1) 性別 - 雄(第2行) 消息 - 「消息」(第3行)

代碼

 $result = ""; 
     $lines = file('sample.txt'); 

     $name= $lines[0]; 
     $age= $lines[1]; 
     $gender= $lines[2]; 
     $message = $lines[3];  

     echo $mail_to; 
     echo $port_num; 
     echo $phone_num; 
     echo $message; 

我希望把每行一個變量是有原因的...... 在我的記事本+查看 - >噓ow Symbol-> Show All Charactes 每行出現「CR LF」 ...但是當其他用戶編輯文本文件時;全線改成「CR」只....

PHP能否處理這個問題???

+0

我不是100%肯定,你想要什麼(n)的答案(縣)? –

+0

也許我的問題是熟悉這個http://stackoverflow.com/questions/817783/read-in-text-file-line-by-line-php-newline-not-being-detected?rq=1 – Bee

+0

但我新手上php – Bee

回答

0
$handle = fopen("sample.txt", "r"); 
if ($handle) { 
    $infos; 
    $i = 0; 
    while (($line = fgets($handle)) !== false) { 
     $info[i++] = $line; 
    } 
    fclose($handle); 
    echo $info[0]; 
    echo $info[1]; 
    echo $info[2]; 
    echo $info[3]; 
} else { 
    // error opening the file. 
} 

從相關的問題:How to read a file line by line in php

+0

是否有任何PHP配置像ini_set(「auto_detect_line_endings」,true); – Bee

+0

我不知道,對不起 – Henry

相關問題