【问题标题】:what is the best way to read a text file avoiding text editor issue in php?什么是读取文本文件的最佳方法,避免 php 中的文本编辑器问题?
【发布时间】:2015-07-21 15:15:36
【问题描述】:

下面的代码正在运行,但是!在其他版本的文本编辑器中编辑示例文本文件时.. 不会的!!

在没有文本编辑器问题的情况下使用 php 逐行阅读文本的最佳方法是什么?

示例文本文件 姓名 - 约翰(第 0 行) 年龄 - 24 岁(第 1 行) 性别 - 男性(第 2 行) message - “消息”(第 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 可以处理这个问题吗???

【问题讨论】:

标签: php


【解决方案1】:
$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

【讨论】:

  • 有没有类似ini_set("auto_detect_line_endings", true);的php配置;
猜你喜欢
  • 1970-01-01
  • 2013-01-01
  • 2011-09-28
  • 1970-01-01
  • 1970-01-01
  • 2021-01-14
  • 2010-10-02
  • 1970-01-01
  • 2011-12-23
相关资源
最近更新 更多