tags:

views:

72

answers:

9
+3  A: 

As I see it, you can just use the existing spaces as delimiters, yielding the following expression:

/([^ ]+) ([^ ]+) (.+)/

That is: three groups separated by spaces, of which the first two groups don’t contain any spaces (but the third may).

Konrad Rudolph
Maybe this notation does not exist in php but wouldn't it be better to do \S+ for non-whitespace instead of [^ ]+
stocherilac
@stocherilac: I prefer to be precise. `\S` matches any non-whitespace, where whitespace may be a space, a tab or a line break. In contrast, my expression *only* allows the space character. Depending on context, `\S` may be preferable but I don’t want to second-guess the exact context.
Konrad Rudolph
@Konrad: Yep I understand that, just checking :)
stocherilac
+7  A: 

You can use this instead of a regex:

$parts = explode(" ", $string, 3);
Sjoerd
This would work if all the fields were in the same format, alas some fields are outside of this format and would give me a dodgy dataset. Using a preg_match allows me to use a conditional statement which is advantageous. Otherwise this would have worked perfectly, thanks.
simnom
A: 
if(preg_match('([0-9/]+ [0-9:]+)', $myString, $regs)) {
  $myTime = strtotime($regs[1]);
}
Palantir
+1  A: 

I think I'll have a go a this

preg_match('|^([0-9]{2})/([0-9]{2})/([0-9]{4})\s([0-9]{2}):([0-9]{2}):([0-9]{2})\s(.*)$|',$str,$matches);
list($str,$d,$m,$y,$h,$m,$s,$comment)=$matches;

you then have the necessary values to reconstruct the time in any format you wish.

jfoucher
That's the kiddie that's going to work best in this situation. Thanks.
simnom
A: 

If you just want to extract it to 2 strings, you can use:

([0-9]{1,2}\/[0-9]{1,2}\/[0-9]{4}\s[0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2})\s(.*)
cmendoza
+1  A: 

Well, if your date/time is stored as type datetime, then you can use something like

preg_match("/^([0-9\\/]{10} [0-9:]{8}) (.*)$/",$str,$matches);
$datetime = $matches[1];
$description = $matches[2];

If your storing the date/time separately, you can use

preg_match("/^([0-9\\/]{10}) ([0-9:]{8}) (.*)$/",$str,$matches);
$date = $matches[1];
$time = $matches[2];
$description = $matches[3];

Of course, an alternative to regular expressions is to explode the string:

list($date,$time,$description) = explode(' ',$str,3);

And another option, assuming the dates and times are always the same length:

$date = substr($str,0,10);
$time = substr($str,11,19);
$description = substr($str,20);
Austin Hyde
+2  A: 

In the circumstances regex is expensive. If this is the format always guaranteed to be there, you could split it by 2 spaces and use the first 2 slices as following:

$str = "10/06/2010 09:10:40 Work not yet started";
$slices = explode(" ", $str, 3);
$timestamp = strtotime($slices[0] . $slices[1]);
echo "String is $str\n";
echo "Timestamp is $timestamp\n";
echo "Timestamp to date is " . strftime("%d.%m.%Y %T", $timestamp) . "\n";
Ain
“In the circumstances regex is expensive” – have you got any benchmarks to back up this outrageous claim? Regular expressions are a lot faster than most people claim/realize, and in particular they perform comparatively well in dynamic languages. And last but not least, that’s a premature optimization. That said, the `explode` option (that has already been mentioned) is a very nice alternative.
Konrad Rudolph
A: 

You can extract the information with the below code:

// sample string you provided
$string = "10/06/2010 09:10:40 Work not yet started";

// regular expression to use
$regex = "/^(\d+)\/(\d+)\/(\d+) (\d+)\:(\d+)\:(\d+) (.+?)$/";

Now, all the fields you'd want is in the array $matches. To extract informations into the array $matches, you can use preg_match()

// method 1: just extract
preg_match($regex, $string, $matches);

// method 2: to check if the string matches the format you provided first
//           then do something with the extracted text
if (preg_match($regex, $string, $matches) > 0) {
   // do something
}

To further use the information you've got:

// to get a Unix timestamp out of the matches
// you may use mktime()

// method 1: supposed your date format above is dd/mm/yyyy
$timestamp = mktime($matches[4], $matches[5], $matches[6], 
  $matches[2], $matches[1], $matches[3]);

// method 2: or if your date format above is mm/dd/yyyy
$timestamp = mktime($matches[4], $matches[5], $matches[6], 
  $matches[1], $matches[2], $matches[3]);

Then you may want to see if the time is correctly parsed:

print date('r', $timestamp)

At last, get the comment like this:

$comment = $matches[7];

Be aware of time zone issue. If you're parsing these data on the same server they're generated, you'd most likely be fine. You might need to add / subtract time from the timestamp above.

Koala Yeung
A: 
$s = '10/06/2010 09:10:40 Work not yet started';
$date = substr($s, 0, 19);
$msg = substr($s, 20);

$date = strtotime($date);
// or
$date = strptime($date, "%m/%d/%Y %H:%M:%S");
Adam Backstrom