SitePoint Sponsor |
|
User Tag List
Results 1 to 9 of 9
-
May 5, 2009, 22:21 #1
- Join Date
- Sep 2002
- Location
- Los Angeles
- Posts
- 1,709
- Mentioned
- 5 Post(s)
- Tagged
- 0 Thread(s)
Trying to Create Regular Expression Pattern
I might be going at this with the wrong tools, so feel free to tell me there's a better way to do it.
I need to extract dates from a string of text, alter the dates and then recompile the string. Here is an example string: DateCreated <= '2009-05-01' AND DateUpdated.
My thought is that if I can create a pattern for 'YYYY-MM-DD' then I could extract the data I need using a regular expression. From there I can convert the extracted string to a date, do my calculation, convert it back to a string and compile the pieces of the array back together with the updated date.
The problem is that I'm utterly confused when it comes to the actual PHP functions in terms of extracting the string and even more so when it comes to creating the pattern to fit 'YYYY-MM-DD' as a date. Has anyone ever done this before or could you point me in the direction of a good tutorial? Everything I manage to find online is a bit over my head and they jump right into complicated examples.TAKE A WALK OUTSIDE YOUR MIND.
-
May 5, 2009, 22:43 #2
- Join Date
- Sep 2002
- Location
- Los Angeles
- Posts
- 1,709
- Mentioned
- 5 Post(s)
- Tagged
- 0 Thread(s)
I think that this pattern will work: \'[0-9]{4}-[0-9]{2}-[0-9]{2}\'
Still, I'm not sure how to use this pattern with the appropriate PHP functions to extract the string(s).TAKE A WALK OUTSIDE YOUR MIND.
-
May 5, 2009, 22:58 #3
- Join Date
- Jul 2006
- Location
- Augusta, Georgia, United States
- Posts
- 4,194
- Mentioned
- 17 Post(s)
- Tagged
- 5 Thread(s)
It depends on what you need to do but the below should put you on the right path.
PHP Code:$matches = array();
$str = 'DateCreated <= \'2009-05-01\' AND DateUpdated';
$exp = preg_match('/^DateCreated\s\<=\s\'([0-9]{4})-([0-1]?[0-9])-([0-1]?[0-9])\'\sAND\sDateUpdated$/',$str,$matches);
echo '<pre>',print_r($matches),'</pre>';
PHP Code:$str = 'DateCreated <= \'2009-05-01\' AND DateUpdated';
$exp = preg_replace('/^DateCreated\s\<=\s\'([0-9]{4})-([0-1]?[0-9])-([0-1]?[0-9])\'\sAND\sDateUpdated$/','$1-$2-$3',$str);
echo '<p>',$exp,'</p>';
PHP Code:$str = 'DateCreated <= \'2009-05-01\' AND DateUpdated';
$date = explode('-',preg_replace('/^DateCreated\s\<=\s\'([0-9]{4})-([0-1]?[0-9])-([0-1]?[0-9])\'\sAND\sDateUpdated$/','$1-$2-$3',$str));
echo '<pre>',print_r($date),'</pre>';
PHP Code:$str = 'DateCreated <= \'2009-05-01\' AND DateUpdated';
$date = array_combine(array('year','month','day'),explode('-',preg_replace('/^DateCreated\s\<=\s\'([0-9]{4})-([0-1]?[0-9])-([0-1]?[0-9])\'\sAND\sDateUpdated$/','$1-$2-$3',$str)));
echo '<pre>',print_r($date),'</pre>';
-
May 5, 2009, 23:06 #4
- Join Date
- Sep 2002
- Location
- Los Angeles
- Posts
- 1,709
- Mentioned
- 5 Post(s)
- Tagged
- 0 Thread(s)
I don't understand this line:
PHP Code:preg_replace('/^DateCreated\s\<=\s\'([0-9]{4})-([0-1]?[0-9])-([0-1]?[0-9])\'\sAND\sDateUpdated$/','$1-$2-$3',$str);
The only part of the string that I'm attempting to extract is the date value itself. Also, the string itself will change regularly but will always have a date value in it.TAKE A WALK OUTSIDE YOUR MIND.
-
May 5, 2009, 23:15 #5
- Join Date
- Jul 2006
- Location
- Augusta, Georgia, United States
- Posts
- 4,194
- Mentioned
- 17 Post(s)
- Tagged
- 5 Thread(s)
Well then it depends but the below should suffice.
PHP Code:$str = 'DateCreated <= \'2009-05-01\' AND DateUpdated';
$date = array_combine(array('year','month','day'),explode('-',preg_replace('/^.*?([0-9]{4})-([0-1]?[0-9])-([0-1]?[0-9]).*?$/','$1-$2-$3',$str)));
echo '<pre>',print_r($date),'</pre>';
-
May 5, 2009, 23:24 #6
- Join Date
- Sep 2002
- Location
- Los Angeles
- Posts
- 1,709
- Mentioned
- 5 Post(s)
- Tagged
- 0 Thread(s)
Thank you for your help, but I'm just not getting it. Maybe I'm not conveying my needs properly...
The string essentially needs to be broken into 3 parts:
1. String of text before the date
2. The date itself
3. String of text after the date
This way I can manipulate the date and then just paste everything back together. To make the matter a bit more confusing, the string may have two dates in which case it would need to be broken into 5 parts:
1. String of text before first date
2. The first date itself
3. String of text after first date and before second date
4. The second date itself
5. String of text after second date
Am I going about this all wrong? I'm not even sure what the best way to tackle this is.TAKE A WALK OUTSIDE YOUR MIND.
-
May 6, 2009, 00:04 #7
- Join Date
- Jul 2006
- Location
- Augusta, Georgia, United States
- Posts
- 4,194
- Mentioned
- 17 Post(s)
- Tagged
- 5 Thread(s)
I think the best way to do that is by a case by case basis and use preg_replace only for the the part of the string which you would like to replace. Otherwise its just a whole bunch of string and array manipulation. If you could provide specific examples that would probably be helpful to everyone viewing this thread.
-
May 6, 2009, 00:24 #8
- Join Date
- Apr 2008
- Location
- North-East, UK.
- Posts
- 6,111
- Mentioned
- 3 Post(s)
- Tagged
- 0 Thread(s)
Morning Jeff,
It appears you're going to need to provide a sample of the source data highlighting the intended match(es) followed by a pseudo 'end-result' indicating the rules to apply.
There is not enough information here to provide you with satisfactory code.
Off Topic:
Congrats on passed your driving test!@AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.
-
May 6, 2009, 00:39 #9
- Join Date
- Apr 2008
- Location
- North-East, UK.
- Posts
- 6,111
- Mentioned
- 3 Post(s)
- Tagged
- 0 Thread(s)
In fact, maybe you were thinking more along these lines?
PHP Code:<?php
$sString = 'This string contains a date, in fact, here it is 2009-12-25. It isnt really Christmas., it is 2009-05-06. Wally.';
echo preg_replace_callback('~[0-9]{4}-[0-9]{2}-[0-9]{2}~', 'hide_crimbo', $sString);
#This string contains a date, in fact, here it is 0000-00-00. It isnt really Christmas., it is 2009-05-06. Wally.
function hide_crimbo($aMatch)
{
#apply business logic and return new date value;
if($aMatch[0] == '2009-12-25')
{
return '0000-00-00';
}
return $aMatch[0];
}
?>
PHP Code:<?php
$sString = 'This string contains a date, in fact, here it is 2009-12-25. It isnt really Christmas., it is 2009-05-06. Wally.';
foreach(preg_split('~([0-9]{4}-[0-9]{2}-[0-9]{2})~', $sString, null, PREG_SPLIT_DELIM_CAPTURE) as $sStringPart)
{
if(1 === preg_match('~([0-9]{4}-[0-9]{2}-[0-9]{2})~', $sStringPart))
{
# apply logic to date field.
if($sStringPart == '2009-12-25')
{
$sStringPart = '0000-00-00';
}
}
# whack it back together.
$sNewString .= $sStringPart;
}
echo $sNewString;
# This string contains a date, in fact, here it is 0000-00-00. It isnt really Christmas., it is 2009-05-06. Wally.
?>@AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.
Bookmarks