Re: [newbie] minor trouble with "while, list and explode" from a text-file

From: Date: Wed, 07 Feb 2001 03:51:46 +0000
Subject: Re: [newbie] minor trouble with "while, list and explode" from a text-file
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-38484@lists.php.net to get a copy of this message
On Wed, 7 Feb 2001 14:14, SED wrote: > Hi, I'm trying to figure out how to do this right: > > I have this text file: > > date 1 | head 1 | text 1 > date 2 | head 2 | text 2 > date 3 | head 3 | text 3 > > And I want to display in a HTML-page like this: > > date 1 > head 1 > text 1 > date 2 > head 2 > text 2 > date 3 > head 3 > text 3 > > And I'm trying to do it with follwing code: > > //sniped > 39> while (list ($line_num, $line) = each ($contentlines)) > 40> { > 41> $message = explode ("|", $line); > 42> > 43> while (list ($date, $head, $text) = each($message)) > 44> { > 45> echo "$date, $head, $text<br>"; > 46> }; > 47> }; > //sniped > > But I get this result: > > Warning: Undefined offset: 2 in c:\inetpub\wwwroot\read_txt_0007.php on > line 43 > > > Can anyone point me to a solution for this? > > Regards, > Sumarlidi Einar Dadason List returns a key and value for each element in the array on which it operates. The array that you get from $message = explode ("|", $line) will be something like KEY VALUE 0 date1 1 head1 2 text1 with spaces around the text (which you may want to chop()) so you need something like 41> $message = explode ("|", $line); 42> 43> while (list (, $value) = each($message)) 44> { 45> echo "chop($value)<br>"; 46> }; The comma in Line 43 is important! Alternatively, you could use str_replace to change the ' | ' (that's space pipe space) to <BR> Cheers -- David Robley | WEBMASTER & Mail List Admin RESEARCH CENTRE FOR INJURY STUDIES | http://www.nisu.flinders.edu.au/ AusEinet | http://auseinet.flinders.edu.au/ Flinders University, ADELAIDE, SOUTH AUSTRALIA

« previous php.general (#38484) next »