Re: Best way to split a string of numbers into segments.
| From: | Marek Kilimajer | Date: | Tue, 02 Dec 2003 14:47:13 +0000 |
| Subject: | Re: Best way to split a string of numbers into segments. | ||
| References: | 1 2 3 4 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-171582@lists.php.net to get a copy of this message | ||
list(, $year, $month, $day) = ....
Tom wrote:
I agree that preg_match generates an array, but I cannot assign to it directly, and in your example I have 4 elements in the array, not three. Array ( [0] => 20031202 [1] => 2003 [2] => 12 [3] => 02 )I suppose something like preg_match("/(\d{4})(\d{2})(\d{2})/", $mydate, list($y,$m$d));Is vaguely what I am gunning for. And substr()... $date = 20031202; $year = substr($date,0,4); $month = substr($date,3,2); $day = substr($date,5,2); That seems like a lot of lines of code, and I am believe that substr() is expensive. Maybe I have been doing too much perl, as it seems such a neat solution to me :Ppreg_match should do it... $mydate = "20031202"; $date = array(); preg_match("/(\d{4})(\d{2})(\d{2})/", $mydate, $date); print_r($date); substr would be much quicker though On Tue, 2003-12-02 at 13:14, Tom wrote:Hi. Is there an easy, non expensive way to do the perl-equivalent of: $date=20031202; ($year, $month, $day) = ($date =~ /(\d{4})(\d{2})(\d{2})/; I looked through the preg_* functions online, but couldn't see anything to help me. Am I stuck with substr() :P ? Thanks.