Words to array function [closed]
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this questionIs there a function to convert a group of words to an array in PHP? The goal of my code is to take an input date and to rearrange it. Would it be best simply to use "explode()"?
yes. explode()
is essentially what you need, then implode()
to get it back into a string.
Something like the following?
$date='2010-10-01'; // YYYY-MM-DD
$dateArray=explode('-',$date); print_r($dateArray);
$dateCattedAgain=implode('-',$dateArray); echo $dateCattedAgain;
You can rearrange the year, month, and day bits as needed.
Cheers!
The best thing to do in this specific situation would be to parse the date and then reformat it as needed. For example:
$d = date_parse('12-12-2009');
print_r($d);
Which yields:
Array
(
[year] => 2009
[month] => 12
[day] => 12
[hour] =>
[minute] =>
[second] =>
[fraction] =>
[warning_count] => 0
[warnings] => Array
(
)
[error_count] => 0
[errors] => Array
(
)
[is_localtime] =>
)
This will also help you catch invalid date ranges.
A function you can use is explode()
. Before PHP version 5.3.0 the function split()
was also useful for this, but it's now deprecated.
精彩评论