• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 277
  • Last Modified:

PHP regex m/d/yyyy parsing

How can i split a string that begins with a date from the remainder of the string.
the month and day can have one or two digits, but the year will always have four, and slash format is always used.
(I'm splitting an RSS feed string)
m/d/yyyy     mm/dd/yyyy   etc.

i could manually use two explodes or strpos and manually/awkardly parse, but i think some version of regex would be better?

thanks!
0
willsherwood
Asked:
willsherwood
  • 3
  • 3
1 Solution
 
Ray PaseurCommented:
Show us an example of the date in situ - there may be a better idea than trying to debug a REGEX.
0
 
willsherwoodAuthor Commented:
4/5/2011 – Congratulations...
11/5/2010 Hanover ...
8/18/2010 FREE...


where ...  is typically another 100-200 chars
0
 
hieloCommented:
preg_split('#\d?\d/\d?\d/\d{4}#', $str);
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
Ray PaseurCommented:
OK, try this:
http://www.laprbass.com/RAY_temp_willsherwood.php
<?php // RAY_temp_willsherwood.php
error_reporting(E_ALL);

// SOME TEST DATA
$dates = array
( '4/27/2011ANDSTUFF'
, '04/01/2010/MORE STUFF'
)
;

// A REGEX
$regex
= '#'           // REGEX DELIMITER
. '\d{1,2}'     // ONE OR TWO DIGITS
. '\/'          // ESCAPED SLASH
. '\d{1,2}'     // ONE OR TWO DIGITS
. '\/'          // ESCAPED SLASH
. '\d{4}'       // 4 DIGITS
. '#'           // END REGEX DELIMITER
;

// TEST EACH CASE
foreach ($dates as $thing)
{
    $new = preg_replace($regex, NULL, $thing);
    echo "<br/>$new";
}

Open in new window

0
 
willsherwoodAuthor Commented:
excellent,  that's it!  and thanks for the comments - helps demystify it.
this gives the rest of the string.
which function to extract the date part (i need to have the date in one var and the rest in the second, which you provided)

thanks!
0
 
Ray PaseurCommented:
This might be a little more on point.  Outputs:
ANDSTUFF CAME WITH DATE STRING 4/27/2011
/MORE STUFF CAME WITH DATE STRING 04/01/2010

Thanks for the points, ~Ray
<?php // RAY_temp_willsherwood.php
error_reporting(E_ALL);

// SOME TEST DATA
$dates = array
( '4/27/2011ANDSTUFF'
, '04/01/2010/MORE STUFF'
)
;

// A REGEX
$regex
= '#'           // REGEX DELIMITER
. '\d{1,2}'     // ONE OR TWO DIGITS
. '\/'          // ESCAPED SLASH
. '\d{1,2}'     // ONE OR TWO DIGITS
. '\/'          // ESCAPED SLASH
. '\d{4}'       // 4 DIGITS
. '#'           // END REGEX DELIMITER
;

// TEST EACH CASE
foreach ($dates as $thing)
{
    $arr = preg_match($regex, $thing, $data);
    $new = preg_replace($regex, NULL, $thing);
    echo "<br/>$new CAME WITH DATE STRING {$data[0]}";
}

Open in new window

0
 
willsherwoodAuthor Commented:
yes yes, perfect.  many thanks!   all set.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now