0

现在我正在使用eregi_replace()已弃用的 function ,我想将其转换为 function preg_match()。现在我有这个:

foreach ($lesson as $key => $val) {
    $lesson_time[$key]->fromTime = eregi_replace('([0-9]{2})([0-9]{2})', '\1:\2',$val->fromTime);
}

其中输入 ($val->fromTime) 是字符串,例如0830or 1150,输出是 08:30 or 11:50。我不擅长正则表达式,所以我想问一下这个具有相同过程的函数如何转换为 preg_match()。

4

3 回答 3

1
$lesson_time[$key]->fromTime = eregi_replace('/([0-9]{2})([0-9]{2})/','$1:$2',$val->fromTime);
于 2014-08-22T13:19:15.020 回答
1
preg_match('/([0-9]{2})([0-9]{2})/', $val->fromTime, $match);

print_r($match);

您不能用 preg_match 替换字符串。您可以使用 preg_replace。

preg_replace('/([0-9]{2})([0-9]{2})/', '$1:$2', $val->fromTime);
于 2014-08-22T13:15:37.363 回答
1

尝试这个

echo preg_replace('/([0-9]{2})([0-9]{2})/', '\\1:\\2',$val->fromTime);
于 2014-08-22T13:16:26.380 回答