SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Zealot darksystem's Avatar
    Join Date
    Jan 2005
    Location
    Fayettville, North Carolina
    Posts
    184
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help with Regular Expression Pattern

    Hi,

    Can you help me with this regular expression pattern.

    #^(\d{4})(\d\d)(\d\d)$#

    Say in a code

    Code:
    preg_match('#^(\d{4})(\d\d)(\d\d)$#', $data['expires'], $matches)
    Can anyone tell me what does it search for that pattern
    Code:
    #^(\d{4})(\d\d)(\d\d)$#
    Any help is highly appreciated.

    Regards,
    Mark
    Ebay API, OSC/CRE/OscMax/ZenCart/SEO Services
    Lucki Multimedia - Email

  2. #2
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,127
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Looks like it is searching for a string that starts with a digit (followed by 3 more digits), then 2 more digits, and ending with 2 more digits (8 digits total).

    It is then grouping them as the first 4 digits in one group (likely to specify a year), followed by a group of 2 digits (likely month), followed by the last group of 2 digits (likely day).

  3. #3
    SitePoint Member
    Join Date
    Jul 2013
    Location
    Brazil
    Posts
    9
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

  4. #4
    SitePoint Zealot darksystem's Avatar
    Join Date
    Jan 2005
    Location
    Fayettville, North Carolina
    Posts
    184
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    @cpradio ;

    Thanks for answering my concern.

    However I have one more concern.

    May I know what could possibly the value of the variable $data['expires'] in
    this code

    preg_match('#^(\d{4})(\d\d)(\d\d)$#', $data['expires'], $matches)

    before it searches the pattern ?

    Coz I dont know what should be the value of $data['expires'] before it does
    search the pattern.

    I thought that the possible value was 2013-08-06 but it doesnt seems right.

    Looking forward to hear from you soon?

    Regards
    Last edited by cpradio; Aug 6, 2013 at 04:11.
    Ebay API, OSC/CRE/OscMax/ZenCart/SEO Services
    Lucki Multimedia - Email

  5. #5
    Avid Logophile silver trophy
    ParkinT's Avatar
    Join Date
    May 2006
    Location
    Central Florida
    Posts
    2,332
    Mentioned
    192 Post(s)
    Tagged
    4 Thread(s)
    To clarify what @cpradio ; already explained,
    • Each \d represents one digit (0 through 9)
    • The \d{4} indicates exactly 4 digits (0 through 9)
    • The segments in parenthesis (called groups) will be provided in the $matches array



    The first index of the array is the entire match, followed by each group: 4-digits then 2-digits then 2-digits. I would expect a 4 element array.

    This indicates the expected pattern for this Regular Expression is in the form 00009944

    As you stated, that would most likely be a date (particularly since the source variable is named 'expires'!)


    Note, also the caret at the beginning and the dollar sign at the end (as @cpradio ; provided it) forces a match of EXACTLY 8 digits and nothing else.
    In other words, if you were to use this Regular Expression to evaluate a string like "I will meet you on 20130415 at dusk." it would not match it.

    Quote Originally Posted by darksystem View Post
    @cpradio ,

    Thanks for answering my concern.

    However I have one more concern.

    May I know what could possibly the value of the variable $data['expires'] in
    this code

    preg_match('#^(\d{4})(\d\d)(\d\d)$#', $data['expires'], $matches)

    before it searches the pattern ?

    Coz I dont know what should be the value of $data['expires'] before it does
    search the pattern.

    I thought that the possible value was 2013-08-06 but it doesnt seems right.

    Looking forward to hear from you soon?

    Regards
    Don't be yourself. Be someone a little nicer. -Mignon McLaughlin, journalist and author (1913-1983)


    Git is for EVERYONE
    Literally, the best app for readers.
    Make Your P@ssw0rd Secure
    Leveraging SubDomains

  6. #6
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,127
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by darksystem View Post
    May I know what could possibly the value of the variable $data['expires'] in
    this code

    preg_match('#^(\d{4})(\d\d)(\d\d)$#', $data['expires'], $matches)

    before it searches the pattern ?

    Coz I dont know what should be the value of $data['expires'] before it does
    search the pattern.

    I thought that the possible value was 2013-08-06 but it doesnt seems right.
    It would have to be 8 numbers/digits (as @ParkinT ; also mentioned), ideally in a specific format yyyyMMdd, for example. So the following would all be valid:
    20130806
    20000714
    20181215
    etc.

    Things that are invalid
    2013-08-06
    2000/07/14
    2018.12.15

  7. #7
    SitePoint Zealot darksystem's Avatar
    Join Date
    Jan 2005
    Location
    Fayettville, North Carolina
    Posts
    184
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks a lot everyone. i got it fixed. i really appreciate your help.-
    Ebay API, OSC/CRE/OscMax/ZenCart/SEO Services
    Lucki Multimedia - Email


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
  •