Re: Explode and Trim

From: Date: Fri, 05 Apr 2002 16:01:37 +0000
Subject: Re: Explode and Trim
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-91727@lists.php.net to get a copy of this message
It works great using the foreach statements, but for some reason couldn't make it work the other way. Oh well, no one ever said there was only one way to do things when programming. Thanks for your help. --- Joshua E Minnie CIO josh@acsurf.net "Don't work for recognition, but always do work worthy of recognition." "Maxim Maletsky" <subscriptions@phpbeginner.com> wrote:> > I think what yo wrote should be working fine for you. > > My way of your code: > > > > > foreach(file($storelist) as $line_num=>$line_data) { > foreach(explode(':', $line) as $key=>$val) { > $val = trim($val); // remove whitespaces around > > // now you are inside each element of your > multidimentional array > // combine your rest of the code and work it out the way > you need it. > > } > } > > > > Sincerely, > > Maxim Maletsky > Founder, Chief Developer > > PHPBeginner.com (Where PHP Begins) > maxim@phpbeginner.com > www.phpbeginner.com > > > > -----Original Message----- > > From: Joshua E Minnie [mailto:josh@acsurf.net] > > Sent: Friday, April 05, 2002 1:07 AM > > To: php-general@lists.php.net > > Subject: [PHP] Explode and Trim > > > > I am parsing through a text file and using explode to convert the > string to > > an array. The problem seems to be at the end of the string, when I > check to > > see if the last element in the array is empty it tells me that it is > not. > > The problem comes because the last element should be empty because all > that > > was after the separator was white space. > > > > ---Sample text file--- > > AL:123 2nd Ave.:SomeCity:(123) 456-7890:(123) 456-1234::: > > MI:293 3rd St.:Another City:(123) 345-2839:(123) 384-0398::: > > MI:437 4th Ave.:Yet Another City:(123) 283-4839:(123) 458-4843::: > > ---End of text file--- > > > > ---Code snippit--- > > $stores = file($storelist); > > for($i=0; $i<count($stores); $i++) { > > //$stores[$i] = trim($stores[$i],"\r"); > > //$stores[$i] = trim($stores[$i],"\n"); > > //$stores[$i] = trim($stores[$i], ":"); > > //$stores[$i] = rtrim($stores[$i]); > > echo $stores[$i]."<br><br>\n"; > > $stores[$i] = explode(":", $stores[$i]); > > } > > > > reset($states); > > while(current($states) && current($stores)) { > > for($i=0; $i<count($stores); $i++, next($stores)) { > > while($stores[$i][0] != key($states)) { > > next($states); > > $state = 0; > > } > > if($state==0) { > > echo > > "<b>".$states[$stores[$i][0]]."</b><br>\n"; > > $state = 1; > > } > > echo $stores[$i][1].", ".$stores[$i][2]."<br>Phone: > > ".$stores[$i][3]."<br>Fax: ".$stores[$i][4]."<br>\n"; > > if(!empty($stores[$i][5])) echo "Email: > > ".$stores[$i][5]."<br>\n"; > > if(!empty($stores[$i][6])) echo "Web site: > ".$stores[$i][6]."<br>\n"; > > if(!empty($stores[$i][7]) && $stores[$i][7] != "") echo > "Additional > > notes: ".$stores[$i][7]."<br>\n"; > > echo "<br>"; > > } > > } > > ---End of code snippit--- > > > > Here is the URL of where the code is being used: > > www.wildwebtech.com/acs/nuven/stores.php. The additional notes should > only > > show up if there were additional notes. > > > > -- > > Joshua E Minnie > > CIO > > josh@acsurf.net > > > > "Don't work for recognition, but always do work worthy of > recognition." > > > > > > > > -- > > PHP General Mailing List (http://www.php.net/) > > To unsubscribe, visit: http://www.php.net/unsub.php > >

« previous php.general (#91727) next »