RE: [PHP] break up variable and put each element in an array

From: Date: Tue, 08 Aug 2006 20:05:00 +0000
Subject: RE: [PHP] break up variable and put each element in an array
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-240335@lists.php.net to get a copy of this message
On Tue, August 8, 2006 9:06 am, Reinhart Viane wrote: >> try this: >> >> $string = "3/01/2005 29/12/2005 2/01/2006 20/02/2006 28/12/2006 >> 1/01/2007 >> 15/02/2007"; >> $array = explode(' ', $string); >> foreach ($array as $value) echo "Date: $value<br />"; > > If the user separates the dates by an enter in the textarea, you need > to > explode on "\r\n". To be able to handle both, you need to use split() > or > preg_split(). > > > When I use > $datelist=$_POST[datelist]; > $string=explode('\r\n',$datelist); '\r\n' and "\r\n" are not anything alike... \r\n only have special meaning inside of ", not ' If you want to cover all browser/OS combinations, you'd want to pre-treat the text area input like: $datelist = $_POST['datelist']; $datelist = str_replace("\r\n", "\r", $datelist); $datelist = str_replace("\r", "\n", $datelist); Now all the date are separated by "\n" and you can reliably use explode("\n", $datelist); on them. -- Like Music? http://l-i-e.com/artists.htm

« previous php.general (#240335) next »