Re: [Problem] - reading file

From: Date: Tue, 07 Feb 2006 01:26:59 +0000
Subject: Re: [Problem] - reading file
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-230016@lists.php.net to get a copy of this message
Hi Roman, This line is your problem. $lines = preg_replace("/\n/", "<br>", $lines); You're replacing your newlines with <br> tags. Your explode won't find anything because it's looking for \n 's. Labunski wrote:
Hello, The problem is, that script outputs Last line from the file (and not first line, as expected)! 4 hours spent seeking for mistake. Hopeless. The script should read all the filenames in the directory, then open each file to read first line from file and output this line. if(!isset($_GET['id'])){//if !ISSET ID $path = "files/info/".LANG; if ($handle = opendir($path)) {//first if
    while (false !== ($file = readdir($handle))) { //while
if ($file != "." && $file != "..") { //if not . ..
    if(!is_file($file)){
     //echo $file."<br><br>";
     $new_path = "files/info/".LANG."/".$file;
     //echo $new_path;
     $text = file($new_path);
     foreach($text as $lines ) {
     $lines = preg_replace("/\n/", "<br>", $lines);
     $lines = trim($lines);
     $lines = explode("\n",$lines);
     $out = "$lines[0]";
     }
     echo $out."<br><br>";
     unset ($out);
    }//!is_file
     }//if not .  ..
    }//while
    closedir($handle);
}//first if }//if !ISSET ID If text files look like: 1.txt{ First line of 1.txt. Second line of 1.txt. } 2.txt{ First line of 2.txt. Second line of 2.txt. } Then Output (using this script) will be: Second line of 1.txt Second line of 2.txt - - - - - - - - - - - But it should be: First line of 1.txt First line of 2.txt I'm really hopeless.. Thanks everyone in advance! Roman.


« previous php.general (#230016) next »