tdavis
03-17-2009, 02:58 PM
What is the equivalent PHP code to split a pipe delimted string?
In Perl, I would do it like this.
@array = split(/\|/, $string);
In Perl, I would do it like this.
@array = split(/\|/, $string);
|
||||
Split pipe delimited stringtdavis 03-17-2009, 02:58 PM What is the equivalent PHP code to split a pipe delimted string? In Perl, I would do it like this. @array = split(/\|/, $string); Fumigator 03-17-2009, 03:54 PM explode() (http://us2.php.net/manual/en/function.explode.php) tdavis 03-17-2009, 06:18 PM Thanks tdavis 03-17-2009, 06:27 PM I am brand new to PHP, so, I am trying a simple script to start off with, using the explode as you suggested; however, I may not be using this in the correct manner. Here is my script, which I want to do nothing more than display the contents of a pipe delimited file. When I run it, it seems to get stuck in a loop. Any advice is appreciated! <?php $file = "groups.txt"; $file = fopen($file, "r") or exit("Unable to open file!"); while(!feof($file)) { echo fgets($file). "<br />"; $array = explode("|", $file); echo "<br>$array[0]"; echo "<br>$array[1]"; echo "<br>$array[2]"; echo "<br>$array[3]"; echo "<br>$array[4]"; echo "<br>$array[5]"; echo "<br>$array[6]"; echo "<br>$array[7]"; } fclose($file); ?> Fumigator 03-17-2009, 06:35 PM You are exploding the file handle, not the data retrieved. Instead of echoing the results of fgets(), assign it to a variable and then use that variable in the explode function. (though, this wouldn't cause an endless loop I wouldn't think, I'm stumped on that one) eak 03-17-2009, 06:49 PM You could also use fgetcsv (http://us.php.net/fgetcsv) to get your data. $handle = fopen("Your.File", "r"); while( ($data = fgetcsv($handle, 1000, "|") ) !== FALSE ){ print_r( $data ); } fclose($handle); tdavis 03-17-2009, 07:06 PM Well, not sure about the loop either, but now it works. This is a little more different from Perl than I thought it would be, not that I am a Perl expert. Thanks again for your help! <?php $file = "groups.txt"; $file = fopen($file, "r") or exit("Unable to open file!"); while(!feof($file)) { $data = fgets($file); // echo fgets($file); $array = explode("|", $data); echo "<br>$array[0]"; echo "<br>$array[1]"; echo "<br>$array[2]"; echo "<br>$array[3]"; echo "<br>$array[4]"; echo "<br>$array[5]"; echo "<br>$array[6]"; echo "<br>$array[7]"; } fclose($file); ?> |
| |||
EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum