PHP 从文件中读取并操作数据


php reading from file and manipulating the data

我在从文本文件中读取信息时遇到一些困难。是否可以使用 php 并一次获取一行,并将该行与变量进行比较,一次一个字符?每次我添加字符搜索算法时,它都会搞砸。还是文件读取只做完整的文件/行/字符前任:

    $file=fopen("text/dialogue.txt","r") or exit("unable to open dialogue file");
    if($file == true) {
        echo "File is open";
        fgets($file);
        $c = "";
        while(!feof($file)) {
            $line = fgets($file)
            while($temp = fgetc($line)) {

               $c = $c . $temp;
               //if statement and comparrison
            }
        }
    } else {
        echo "File not open";
        }
    fclose($file);

您可以使用php文件函数逐行读取文件

<?php
   $lines = file("myfile.txt");
   foreach($lines as $line){
       ## do whatever you like here
       echo($line);
   }
?> 

请查看 php 手册

http://php.net/manual/en/function.file.php