zoukankan      html  css  js  c++  java
  • Read in text file line by line php newline not being detected

    I have a php function I wrote that will take a text file and list each line as its own row in a table.

    The problem is the classic "works fine on my machine", but of course when I ask somebody else to generate the .txt file I am looking for, it keeps on reading in the whole file as 1 line.  When I open it in my text editor, it looks just as how I would expect it with a new name on each line, but its the newline character or something throwing it off.

    So far I have come to the conclusion it might have something to do with whatever text editor they are using on their Mac system.

    Does this make sense? and is there any easy way to just detect this character that the text editor is recognizing as a new line and replace it with a standard one that php will recognize?

    UPDATE: Adding the following line solved the issue.

    ini_set('auto_detect_line_endings',true);

    Function:

    function displayTXTList($fileName) {

        if(file_exists($fileName)) {

            $file = fopen($fileName,'r');

            while(!feof($file)) {

                $name = fgets($file);

                echo('<tr><td align="center">'.$name.'</td></tr>');

            }

            fclose($file);

        } else {

            echo('<tr><td align="center">placeholder</td></tr>');

        }      

    }

  • 相关阅读:
    BZOJ 3390 Bad Cowtractors牛的报复
    BZOJ 4291 Kieszonkowe
    【重温基础】3.循环和迭代
    【重温基础】3.循环和迭代
    【重温基础】2.流程控制和错误处理
    【重温基础】2.流程控制和错误处理
    【重温基础】1.语法和数据类型
    【重温基础】1.语法和数据类型
    vue axios全攻略
    vue axios全攻略
  • 原文地址:https://www.cnblogs.com/weaver1/p/2500722.html
Copyright © 2011-2022 走看看