Re: break up variable and put each element in an array
| From: | Ivo F.A.C. Fokkema | Date: | Tue, 08 Aug 2006 13:56:49 +0000 |
| Subject: | Re: 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-240316@lists.php.net to get a copy of this message | ||
On Tue, 08 Aug 2006 14:50:53 +0100, Dave Goodchild wrote:
> On 08/08/06, Reinhart Viane <rv@domos.be> wrote:
>>
>> A. I have a page on which people can supply dates in a text area. Dates
>> are
>> entered like this:
>> 3/01/2005
>> 29/12/2005
>> 2/01/2006
>> 20/02/2006
>> 28/12/2006
>> 1/01/2007
>> 15/02/2007
>>
>> B. Now I need this Post element to be broken into pieces (per date) and
>> each
>> of those pieces should be put into a text so the outcome (echo on screen)
>> would be like this:
>> Date=3/01/2005
>> Date=29/12/2005
>> Date=2/01/2006
>> Date=20/02/2006
>> Date=28/12/2006
>> Date=1/01/2007
>> Date=15/02/2007
>>
>> The posted variable from A looks like this (when written on screen):
>> 3/01/2005 29/12/2005 2/01/2006 20/02/2006 28/12/2006 1/01/2007 15/02/2007
>>
>> So I need break this up and store any individual date into a key of an
>> array
>> Afterwards I need to loop through this array and use the value of each key
>> to be printed after 'Date='
>>
>
> 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().