Re: [PHP] Best way to split a string of numbers into segments.

2003-12-02 Thread Mike
preg_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.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Best way to split a string of numbers into segments.

2003-12-02 Thread David Otton
On Tue, 02 Dec 2003 13:14:20 +, you wrote:

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 ?

list() springs to mind

list ($a, $b, $c) = preg_split (/* mumble mumble */);

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Best way to split a string of numbers into segments.

2003-12-02 Thread Tom
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 :P

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

 

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: [PHP] Best way to split a string of numbers into segments.

2003-12-02 Thread Tom
David Otton wrote:

On Tue, 02 Dec 2003 13:14:20 +, you wrote:
 

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 ?
   

list() springs to mind

list ($a, $b, $c) = preg_split (/* mumble mumble */);
 

Hi David.

Yes, list() was, initially, my starting point. But preg_split() will not 
allow me split with the
full /(\d{4})(\d{2})(\d{2})/ syntax (and assign the temp vars to thos in 
list().
Unless I missed something?

$date=20031202;
list ($a, $b, $c) = preg_split (/(\d{4})(\d{2})(\d{2})/);
 parse error, unexpected '/', expecting ')'
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: [PHP] Best way to split a string of numbers into segments.

2003-12-02 Thread Mike
list ($a, $b, $c) = preg_split (/(\d{4})(\d{2})(\d{2})/);

note the 's

 list ($a, $b, $c) = preg_split (/(\d{4})(\d{2})(\d{2})/);
   parse error, unexpected '/', expecting ')'

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Best way to split a string of numbers into segments.

2003-12-02 Thread Marek Kilimajer
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 :P

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


 


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: [PHP] Best way to split a string of numbers into segments.

2003-12-02 Thread Mike
From the manual preg_split

Returns an array containing substrings of subject split along boundaries
matched by pattern.

since you dont have any borders, this is unlikely to help you

the only was i can see to do it is a quite long way

$mydate = 20031202;
$date = array();
preg_match(/(\d{4})(\d{2})(\d{2})/, $mydate, $date);
list($x, $y, $d, $m) = $date;
echo $d. / .$m. / .$y .\n;

Mike

On Tue, 2003-12-02 at 15:15, Tom wrote:
 I have noted the 's.
 Could you send me a complete piece of code, please?
 I cannot for the life of me get it to work, at all.
 
 Mike wrote:
 
 list ($a, $b, $c) = preg_split (/(\d{4})(\d{2})(\d{2})/);
 
 note the 's
 
   
 
 list ($a, $b, $c) = preg_split (/(\d{4})(\d{2})(\d{2})/);
   parse error, unexpected '/', expecting ')'
 
 
 
   
 
 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php