SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    Visible Ninja bronze trophy
    JeffWalden's Avatar
    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.

  2. #2
    Visible Ninja bronze trophy
    JeffWalden's Avatar
    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.

  3. #3
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,146
    Mentioned
    16 Post(s)
    Tagged
    3 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>'
    The below will give you just the date string which you could then use explode on to extract the year,month and day.

    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>'
    Here is the entire thing resulting in an array for the year,day,month.

    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>'
    You could also do something a little fancier which maps the associative keys.

    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>'

  4. #4
    Visible Ninja bronze trophy
    JeffWalden's Avatar
    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); 
    I think the part I don't understand is where you're getting the value: /^DateCreated\s\<=\s\'([0-9]{4})-([0-1]?[0-9])-([0-1]?[0-9])\'\sAND\sDateUpdated$/ from.

    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.

  5. #5
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,146
    Mentioned
    16 Post(s)
    Tagged
    3 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>'
    The parenthesis capture the matches which are then referenced in the replacement string using the index starting at 1via $1,$2,$3.

  6. #6
    Visible Ninja bronze trophy
    JeffWalden's Avatar
    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.

  7. #7
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,146
    Mentioned
    16 Post(s)
    Tagged
    3 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.

  8. #8
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    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.

  9. #9
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    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];
    }
    ?>
    Or

    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})~'$sStringnullPREG_SPLIT_DELIM_CAPTURE) as $sStringPart)
    {
        if(
    === 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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •