#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2008
    Posts
    152
    Rep Power
    0

    Isolating results based on regular expressions


    I'm currently retruning random data from DOM Parser. The only thing that I want to keep out of all the data is the dates. For example, everything below may varry except the FORMAT OF THE DATES:

    aja ajlshfs d sd sdjfh sd fsd 2008/02/06 aklsd as dklas a sdkajs aslkd as da 2008/06/08 aslijas as asldhasjda sd asj as dasjdha sd 2008/11/12:

    Is there a function I can use to say get rid of everyhting and leave me with anything matching the regular expression below. The key here is that the information surrounding the dates is not consistent. The FORMAT OF THE DATES is the only thing consistent. I want to isolate based on the pattern below:

    PHP Code:
    $pattern '|^\d{4}/\d{1,2}/\d{1,2}$|'
    As a result, I'm left with:

    Code:
    2008/02/06
    2008/06/08
    2008/11/12
  2. #2
  3. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,992
    Rep Power
    9397
    I don't see where the problem is.
    - You have a string
    - It has dates
    - You want only the dates
    - You've already extracted the dates
    So... just forget the original string and keep the dates.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2008
    Location
    VA
    Posts
    2
    Rep Power
    0
    PHP Code:
    <?php

    $string 
    'aja ajlshfs d sd sdjfh sd fsd 2008/02/06 aklsd as dklas a sdkajs aslkd as da 2008/06/08 aslijas as asldhasjda sd asj as dasjdha sd 2008/11/12';

    $pattern '~(19|20)[0-9]{2}[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])~';

    preg_match_all($pattern$string$matches);

    print_r($matches);
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2008
    Posts
    152
    Rep Power
    0
    I haven't already extracted the dates. I just provided that as the end-result I'm looking for. The problem is that the string of text is not constant. The only thing constant is the FORMAT FOR TEH DATE.

    Originally Posted by requinix
    I don't see where the problem is.
    - You have a string
    - It has dates
    - You want only the dates
    - You've already extracted the dates
    So... just forget the original string and keep the dates.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2008
    Posts
    152
    Rep Power
    0
    As I mentioned in my post, I can't use this method because $string is not consistent; $string, being the text returned from the DOM parser. The only thing consistent is the DATE FORMAT.

    Originally Posted by ahowell
    PHP Code:
    <?php

    $string 
    'aja ajlshfs d sd sdjfh sd fsd 2008/02/06 aklsd as dklas a sdkajs aslkd as da 2008/06/08 aslijas as asldhasjda sd asj as dasjdha sd 2008/11/12';

    $pattern '~(19|20)[0-9]{2}[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])~';

    preg_match_all($pattern$string$matches);

    print_r($matches);
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2008
    Location
    VA
    Posts
    2
    Rep Power
    0
    cd_gary. Are you aware of how regular expression works? using preg_match_all() with the pattern given will find all of the dates in the format that you want, out of any text... doesn't matter how inconsistent it is...
  12. #7
  13. No Profile Picture
    Spontaneously Present
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jul 2005
    Posts
    1,149
    Rep Power
    0
    if what youre looking for is consistent than its more than possible. you dont care about the chaos around the consistency...

    put together an array of known date formats and just cycle through them (redundant)

    or build the formats dynamically.

IMN logo majestic logo threadwatch logo seochat tools logo