Re: Re: Can PHP exclude % characters in external txt file?

From: Date: Tue, 14 Aug 2001 14:35:15 +0000
Subject: Re: Re: Can PHP exclude % characters in external txt file?
References: 1  Groups: php.db 
Request: Send a blank email to php-db+get-11336@lists.php.net to get a copy of this message
On Mon, 13 Aug 2001, Phillip Bow wrote: > I can't think of a simple way to do it off of an include, but off the top of > my head: > <?php > $file = file("data.txt"); > for($x=0; $x<count($file); $x++){ > $temp = str_replace("%", "", $file[$x]); > print "$temp\n"; > } > unset($file, $temp); > ?> > -- Few things I think I should point out here. The first being that str_replace() will replace -every- percent sign in the line, not just the one that occurs as the first character. If you know for sure that the the first character is always a % and you don't want to print it just use: print substr($line, 1); as the body of your for loop. Secondly, you're callilng count() for every iteration of your for loop. Might as well call it once, store the variable and save the computation time of repeatdly calling it. Thirdly, and this one is perhaps a bit anal retentive, PHP treats arrays as dynamically growing linked lists. So, when you call an array by it's subscript the php engine starts at the head and walks X steps through the list to get what you wanted. If you don't plan on re-using the array, It'd be better to do something like array_shift() to get values from it. Perhaps something like this would work better: > $file = file("data.txt"); > for($x=0; $x<count($file); $x++){ > $temp = str_replace("%", "", $file[$x]); > print "$temp\n"; > } > unset($file, $temp); > ?> $file = file("data.txt"); $line = array_shift($file); while ($line != NULL) { print substr($line, 1); $line = array_shift($file); } unset($file, $line); Note: I've never done file I/O w/ PHP... I'm just assuming that the original post was correct there. Justin Buist Trident Technology, Inc. 4700 60th St. SW, Suite 102 Grand Rapids, MI 49512 Ph. 616.554.2700 Fx. 616.554.3331 Mo. 616.291.2612

« previous php.db (#11336) next »