开发者

Words to array function [closed]

Closed. This question needs details or clarity. It is not currently accepting answers. 开发者_JAVA技巧

Want to improve this question? Add details and clarify the problem by editing this post.

Closed 4 years ago.

Improve this question

Is 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜