?
Solved

PHP regex   m/d/yyyy  parsing

Posted on 2011-04-27
7
Medium Priority
?
275 Views
Last Modified: 2012-08-13
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
Comment
Question by:willsherwood
  • 3
  • 3
7 Comments
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 35476975
Show us an example of the date in situ - there may be a better idea than trying to debug a REGEX.
0
 

Author Comment

by:willsherwood
ID: 35476998
4/5/2011 – Congratulations...
11/5/2010 Hanover ...
8/18/2010 FREE...


where ...  is typically another 100-200 chars
0
 
LVL 82

Expert Comment

by:hielo
ID: 35477035
preg_split('#\d?\d/\d?\d/\d{4}#', $str);
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 35477037
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
 

Author Closing Comment

by:willsherwood
ID: 35477057
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
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 35477102
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
 

Author Comment

by:willsherwood
ID: 35478026
yes yes, perfect.  many thanks!   all set.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses
Course of the Month14 days, 13 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question