How can I split a 6 digit number in to 2 separate numbers?

General discussions related to php

Moderators: egami, macek, gesf

Post Reply
spyros
New php-forum User
New php-forum User
Posts: 18
Joined: Thu Aug 25, 2005 1:49 pm

Sun Jul 22, 2007 1:03 am

Hello
I have a 6 digit number and I want to split it to 2 separate numbers do you know how can I do this ?
For example the number 200509 I want to split it in to 2005 and 09.

Thank you.
User avatar
WiZARD
Moderator
Moderator
Posts: 1240
Joined: Thu Jun 20, 2002 10:14 pm
Location: Ukraine, Crimea, Simferopol
Contact:

Mon Jul 23, 2007 2:54 am

spyros wrote:Hello
I have a 6 digit number and I want to split it to 2 separate numbers do you know how can I do this ?
For example the number 200509 I want to split it in to 2005 and 09.

Thank you.
http://ua2.php.net/manual/en/function.preg-split.php
see example.
"Sex,Drugs and Rock&Roll " replaced at "Sucks,Bugs and Plug&Play";
Image
User avatar
AeroX
php-forum Fan User
php-forum Fan User
Posts: 140
Joined: Sun May 11, 2008 1:40 am
Location: London, UK
Contact:

Wed May 14, 2008 12:35 pm

If it's always a constant pattern you could do it without using preg.

i.e. if its a date with out spaces 20080514 (14/05/2008)

Code: Select all

<?php

$datestring = "20080514";

$year = substr( $datestring, 0, 4 );
$month = substr( $datestring, 4, 2 );
$day = substr( $datestring, 6, 2 );

?>
AeroX.

When posting code to be reviewed please enclose it in the [ code ] [ /code ] tags as it makes it a lot easier for people to read as it correctly formats itself on screen.

Image
    Click Here to Visit[/url]
    Post Reply