PHP正则表达式提取时间戳和注释

时间:2010-06-21 12:35:13

标签: php regex

我有一些来自旧的访问数据库的导出文本字段,这些字段被移植到一个新的mysql结构中。格式有各种字段输入:

10/06/2010 09:10:40工作尚未开始

我想获取该字符串并使用某种正则表达式来提取日期/时间信息,然后提取注释。

是否有简单的正则表达式语法来匹配此信息?

由于

10 个答案:

答案 0 :(得分:7)

您可以使用此代替正则表达式:

$parts = explode(" ", $string, 3);

答案 1 :(得分:3)

在我看来,你可以使用现有的空格作为分隔符,产生以下表达式:

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

即:由空格分隔的三个组,其中前两个组不包含任何空格(但第三个可以包含空格)。

答案 2 :(得分:3)

我想我会去看看这个

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;

然后你有了必要的值来重建你想要的任何格式的时间。

答案 3 :(得分:2)

在这种情况下,正则表达式很昂贵。如果这是始终保证存在的格式,您可以将其拆分2个空格并使用前2个切片如下:

$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";

答案 4 :(得分:1)

好吧,如果您的日期/时间存储为datetime类型,那么您可以使用类似

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

如果您单独存储日期/时间,则可以使用

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

当然,正则表达式的替代方法是爆炸字符串:

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

另一种选择,假设日期和时间总是相同的长度:

$date = substr($str,0,10);
$time = substr($str,11,19);
$description = substr($str,20);

答案 5 :(得分:0)

if(preg_match('([0-9/]+ [0-9:]+)', $myString, $regs)) {
  $myTime = strtotime($regs[1]);
}

答案 6 :(得分:0)

如果您只想将其提取为2个字符串,则可以使用:

([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(.*)

答案 7 :(得分:0)

您可以使用以下代码提取信息:

// 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+) (.+?)$/";

现在,您想要的所有字段都在数组$ matches中。 要将信息提取到数组$ matches中,可以使用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 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]);

然后你可能想看看是否正确解析了时间:

print date('r', $timestamp)

最后,得到这样的评论:

$comment = $matches[7];

请注意时区问题。如果您在生成的同一台服务器上解析这些数据,那么您很可能会很好。您可能需要从上面的时间戳添加/减去时间。

答案 8 :(得分:0)

$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");

答案 9 :(得分:0)

是否存在用于匹配此信息的简单正则表达式语法?

是的。就在这里。这是“提取”而不是“验证”的练习。您只希望在立即跟在datetime表达式后面的空格上仅对字符串进行一次拆分,以恰好形成两个元素。匹配日期,然后是空格,然后是时间,然后忘记所有匹配的内容(\K元字符-重新开始全字符串匹配),然后匹配要用作定界符的空格。

限制爆炸,即使注释中有空格,也仅生成两个元素。

代码:(Demo

$string = '10/06/2010 09:10:40 Work not yet started';
var_export(preg_split('/\S+ \S+\K /', $string, 2));

输出:

array (
  0 => '10/06/2010 09:10:40',
  1 => 'Work not yet started',
)

不需要捕获组,preg_match()不太理想,因为它会在其输出中创建多余的数据。 preg_split()是最直接提供所需输出的单功能技术。如果这是我的项目,那么我不会做任何其他事情。