使用PHP将字符串拆分为日期和两个整数

时间:2014-01-24 09:58:21

标签: php html string

我的字符串格式如此'2014-07-11-4-8'。 '2014-07-11'部分是日期,'4'是整数,'8'是整数。如何使用PHP将原始字符串拆分为这3个变量?

2 个答案:

答案 0 :(得分:3)

尝试使用正则表达式:

$string = '2014-07-11-4-8';

preg_match("/(\d{4}-\d{2}-\d{2})-(\d+)-(\d+)/", $string, $matches);
print_r($matches);

$date = $matches[1];
$int1 = $matches[2];
$int2 = $matches[3];

答案 1 :(得分:-1)

$date  = "2014-07-11-4-8";
$array = explode("-", $date);

然后你可以访问数组变量

中的每个元素