$re = '/(?(DEFINE)
(?<int>
(?:
\G|
(?!\n)
)
\s*\b\d{1,5}\s*
)
(?<timepart>
(
s|
m|
h|
d
)
\b
)
)
^
((?&int))
((?&timepart))
(?<string>.+)
$/xim';
$str = '2min baz
12 mins foo
1 hour 20 mins remind this
1sec
5 hours
1 sec foo
3 minutes 4 seconds
3333 sec do
1 hrs 20 mins 5 seconds remind this
2 days 3 hrs 20 mins 5 seconds remind this
foo 2sec bar //fail
2 s hi
3 m foo
3m bar
2d baz
9 h foobar';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
// Print the entire match result
var_dump($matches);
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for PHP, please visit: http://php.net/manual/en/ref.pcre.php