Here it is debugged (still has some corner cases though):
(And thanks Sasha--it's always fun)
<?
echo time2seconds("abc 4:10 4:10:20 4:10:20.30\n");
/*****************************************************************************
*
* time2seconds()
*
* Given a string that could contain arithmetic expressions, with the
* addition that numeric constants could be potentially expressed as
* times, e.g 1:36 for 96 seconds, or 2:10:08 for 2 hours 10 minutes and
* 8 seconds, also decimal fractions after seconds are allowed, e.g
* 3:45.6 or 3:40:50.67, replace all the time values with their
* equivalent number of seconds.
*
****************************************************************************/
function time2seconds($str) {
$hr = 0;
$sec = 0;
$min = 0;
$dec = 10;
$ret = "";
$in_digit = false;
$in_time = false;
for ($i = 0, $end = strlen($str); $i < $end; $i++) {
$c = $str[$i];
if ('0' <= $c && $c <= '9') {
$sec = $sec * 10 + $c;
$in_digit = true;
} else if ($in_digit && $c == ':') {
$hr = $min;
$min = $sec;
$sec = 0;
$in_time = true;
} else if ($in_time && $c == '.') {
$sec += $c / $dec;
$dec *= 10;
} else if ($in_time) {
$tmp = $hr * 3600 + $min * 60 + $sec;
$ret .= "$tmp$c";
$in_digit = false;
$in_time = false;
$hr = 0;
$min = 0;
$sec = 0;
$dec = 10;
} else if ($in_digit) {
$ret .= "$sec$c";
$in_digit = false;
$hr = 0;
$min = 0;
$sec = 0;
$dec = 10;
} else {
$ret .= $c;
$hr = 0;
$min = 0;
$sec = 0;
$dec = 10;
}
//echo "time: [$hr:$min:$sec $dec] ret: [$ret]\n";
}
return $ret;
}
/*
PLUG: http://plug.org, #utah on irc.freenode.net
Unsubscribe: http://plug.org/mailman/options/plug
Don't fear the penguin.
*/