在php中读取文本文件避免文本编辑器问题的最佳方法是什么?


what is the best way to read a text file avoiding text editor issue in php?

下面的代码可以工作,但是!当示例文本文件在其他版本的文本编辑器中编辑时…它不会! !

什么是最好的方式来阅读文本逐行没有文本编辑器问题使用php?

示例文本文件姓名- John(第0行)年龄- 24岁(第一行)性别-男性(第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;

我想把每一行放在一个变量中是有原因的…在我的记事本++ +视图->显示符号->显示所有字符每行出现"CR LF"…但当其他用户编辑文本文件时;所有行更改为"CR"只....

PHP可以处理这个问题吗??

$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.
} 

来自相关问题:如何在php中逐行读取文件