Jump to content

[SOLVED] Sorting dates out of a string of text...


ShootingBlanks

Recommended Posts

Here's my problem.  It may be too complex to offer a full solution for me here (if you can, though, that's great!), but if anyone could even point me in the right direction of where to start and what PHP commands to be looking into (via the PHP manual or whatever), that would be perfect!...

 

I have a column in a database that holds a string of text.  It starts with a date in YYYY-MM-DD format, then it is followed by any number of other characters.  Now, sometimes there will be (in that same string) another date (YYYY-MM-DD) followed by more characters.  If that is the case, the second date will be preceeded by four ampersands (&&&&).  So, here are some examples of what the string could be:

  • 2007-12-24: It is Christmas.
  • 2007-08-01: Today is the first day in August.&&&&2007-07-01: Today is the first day in July.
  • 2007-11-01: Here is another example&&&&2007-10-22: More text&&&&2007-09-05: Still more text

 

Now, what I want to do is this...

 

...when I pull one of those records (via a SQL query) I want to get only the part of the record that fall between certain dates.  So, for example, let's work with the third string above.  I may only want portions of that string that fall between 2007-10-01 and 2008-01-01.  In that case, the third part of the string (beginning with "&&&&2007-09-05" wouldn't be pulled (and put into a string variable or whatever).

 

So, what I would get with my record would just be:

 

2007-11-01: Here is another example&&&&2007-10-22: More text

 

Similarly, I may want only that part of the string that falls between 2007-01-01 and 2007-11-01, in which case I'd only retrieve:

 

2007-10-22: More text&&&&2007-09-05: Still more text

 

I should also note that every dated element in the original string will be in decending order.  So, you'd never have a string that would jump around and be out of order, like:

 

2007-01-01: Some text from 2007&&&&2006-01-01: Some text from 2006&&&&2008-01-01: Jumping into the future

 

I know this may sound confusing.  I hope it made sense.  Any suggestions/ideas???

 

Thanks!!!

Link to comment
Share on other sites

This should do what you're after, if you don't understand how it works please say and I'll explain!

<?php
$from = "2007-10-01";
$to   = "2008-01-01";
$str  = "2007-11-01: Here is another example&&&&2007-10-22: More text&&&&2007-09-05: Still more text";
$out  = preg_split("/(\d{4}-\d{2}-\d{2}): /",str_replace("&&&&","",$str),-1,PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY);

foreach($out as $k => $v) {
    if(preg_match("/^\d{4}-\d{2}-\d{2}$/",$v) && $v >= $from && $v <= $to) {
        echo "$v: ".$out[$k+1]." is between $from and $to.<br />";
    }
}
?>

Link to comment
Share on other sites

Indeed, I don't!  ;)

 

Here's a commented version that explains it all!

<?php
$from = "2007-10-01"; //Set our start date
$to   = "2008-01-01"; //Set our end date
$str  = "2007-11-01: Here is another example&&&&2007-10-22: More text&&&&2007-09-05: Still more text"; //The string

/*Build an array ($out) containing the date and then the text in their own keys. I stripped out the "&&&&" first 
with str_replace() as this could prove problematic. "/(\d{4}-\d{2}-\d{2}): /" is a regular expression, used for 
pattern matching, in this case \d represents a digit and the number in curly brackets says how many digits 
we're looking for, split by hyphens and having a colon and space at the end. The round brackets are to mark 
the part of the pattern that we also want to keep with the PREG_SPLIT_DELIM_CAPTURE flag, other wise it will 
act as a delimiter and not store it. PREG_SPLIT_NO_EMPTY gets rid of any empty matches. */

$out  = preg_split("/(\d{4}-\d{2}-\d{2}): /",str_replace("&&&&","",$str),-1,PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY);


foreach($out as $k => $v) { //Loop through our array of matches, returning both the key and value on each pass
    /*Use a similar expression as before to check if the current key contains a date and it lands between our 
    $from and $to values*/
    if(preg_match("/^\d{4}-\d{2}-\d{2}$/",$v) && $v >= $from && $v <= $to) {
        //If we have a match, display it along with the text that accompanies it in the next array key
        echo "$v: ".$out[$k+1]." is between $from and $to.<br />";
    }
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.