PHP正则表达式输出转换

发布于 2024-12-07 02:40:03 字数 1137 浏览 1 评论 0原文

我想将 12h 34m 45s 这样的输出转换为 12:34:45 ,

如果其中一个返回为空,则应该可以忽略它。 因此,34m 45s 应该是 00:34:45,当然,个位数应该是可能的,例如 1h 4m 1s,以及个位数和两位数的组合,例如 12h 4m 12s 等等。

有人可以帮忙吗?

这是实际的代码

$van = $_POST['gespreksduur_van']; $tot = $_POST['gespreksduur_tot']; $regex = '/(\d\d?h ?)?(\d\d?m ?)?(\d\d?s)?/';

        if(preg_match($regex, $van, $match) AND preg_match($regex, $tot, $matches))
        {
            for ($n = 1; $n <= 3; ++$n) { if (!array_key_exists($n, $match)) $match[$n] = 0; }
            for ($i = 1; $i <= 3; ++$i) { if (!array_key_exists($i, $matches)) $matches[$i] = 0; }

            $van = printf("%02d:%02d:%02d", $matches[1], $matches[2], $matches[3]);
            $tot = printf("%02d:%02d:%02d", $match[1], $match[2], $match[3]);

            print($van);
            print($tot);

            $data['gespreksduurvan'] = htmlspecialchars($van);
            $data['gespreksduurtot'] = htmlspecialchars($tot);

            $smarty->assign('gsv',$data['gespreksduurvan']);
            $smarty->assign('gst',$data['gespreksduurtot']);
        }

I want to turn a output like 12h 34m 45s to 12:34:45

also it should be possible if one o these is returned empty is will ignore it.
So 34m 45s should be 00:34:45 and off course single digits should bee possible like 1h 4m 1s and a combo off single and double digits like 12h 4m 12s and so on.

Can someone please help ?

This is the actual code

$van = $_POST['gespreksduur_van'];
$tot = $_POST['gespreksduur_tot'];
$regex = '/(\d\d?h ?)?(\d\d?m ?)?(\d\d?s)?/';

        if(preg_match($regex, $van, $match) AND preg_match($regex, $tot, $matches))
        {
            for ($n = 1; $n <= 3; ++$n) { if (!array_key_exists($n, $match)) $match[$n] = 0; }
            for ($i = 1; $i <= 3; ++$i) { if (!array_key_exists($i, $matches)) $matches[$i] = 0; }

            $van = printf("%02d:%02d:%02d", $matches[1], $matches[2], $matches[3]);
            $tot = printf("%02d:%02d:%02d", $match[1], $match[2], $match[3]);

            print($van);
            print($tot);

            $data['gespreksduurvan'] = htmlspecialchars($van);
            $data['gespreksduurtot'] = htmlspecialchars($tot);

            $smarty->assign('gsv',$data['gespreksduurvan']);
            $smarty->assign('gst',$data['gespreksduurtot']);
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦归所梦 2024-12-14 02:40:03

您可以使用正则表达式提取组件,然后使用 printf() 按照您喜欢的方式格式化组件:

$time = "1h 45m";
preg_match("/(\d\d?h ?)?(\d\d?m ?)?(\d\d?s)?/", $time, $matches);
for ($i = 1; $i <= 3; ++$i) { if (!array_key_exists($i, $matches)) $matches[$i] = 0; }
printf("%02d:%02d:%02d", $matches[1], $matches[2], $matches[3]);

正则表达式允许可选组件。 for 循环只是用默认值零填充任何丢失的键(以防止秒或分钟/秒都丢失时出现未定义的键错误)。 printf 总是打印所有带有两个零的组件。

You can use a regex to extract the components, and then a printf() to format the components how you like:

$time = "1h 45m";
preg_match("/(\d\d?h ?)?(\d\d?m ?)?(\d\d?s)?/", $time, $matches);
for ($i = 1; $i <= 3; ++$i) { if (!array_key_exists($i, $matches)) $matches[$i] = 0; }
printf("%02d:%02d:%02d", $matches[1], $matches[2], $matches[3]);

The regex allows optional components. The for loop there just fills any missing keys with a default value of zero (to prevent undefined key errors when seconds, or both minutes/seconds are missing). The printf always prints all components with two zeros.

将军与妓 2024-12-14 02:40:03

如果你想使用正则表达式,你可以使用 preg_replace_callback

    <?php


function callback($matches)
{   
    var_dump($matches);
    if ($matches[2] == "") $matches[2] = "00";
    if ($matches[4] == "") $matches[4] = "00";
    if ($matches[6] == "") $matches[6] = "00";
    return $matches[2] . ":" . $matches[4] . ":" . $matches[6];
}   

$str = "12h 34m 45s";
$str = preg_replace_callback("`(([0-2]?[0-9])h )?(([0-5]?[0-9])m )?(([0-5]?[0-9])s)?`", "callback", $str, 1); 

echo $str;

If you want to use regex, you can use preg_replace_callback

    <?php


function callback($matches)
{   
    var_dump($matches);
    if ($matches[2] == "") $matches[2] = "00";
    if ($matches[4] == "") $matches[4] = "00";
    if ($matches[6] == "") $matches[6] = "00";
    return $matches[2] . ":" . $matches[4] . ":" . $matches[6];
}   

$str = "12h 34m 45s";
$str = preg_replace_callback("`(([0-2]?[0-9])h )?(([0-5]?[0-9])m )?(([0-5]?[0-9])s)?`", "callback", $str, 1); 

echo $str;
杀お生予夺 2024-12-14 02:40:03

这是无需正则表达式即可执行此操作的方法

// function for your purpose
function to_time($str){
    $time_arr = explode(' ', $str);
    $time_h = '00';
    $time_m = '00';
    $time_s = '00';
    foreach($time_arr as $v){
        switch(substr($v, -1)){
            case 'h': $time_h = intval($v); break;
            case 'm': $time_m = intval($v); break;
            case 's': $time_s = intval($v); break;
        }
    }
    return $time_h . ':' . $time_m . ':' . $time_s;
}

//test array
$time[] = '1h 45s';
$time[] = '12h 35m 45s';
$time[] = '1m 45s';

// output
foreach($time as $t)
    var_dump(to_time($t));

此输出

string '1:00:45' (length=7)
string '12:35:45' (length=8)
string '00:1:45' (length=7)

this is how you can do this without regex

// function for your purpose
function to_time($str){
    $time_arr = explode(' ', $str);
    $time_h = '00';
    $time_m = '00';
    $time_s = '00';
    foreach($time_arr as $v){
        switch(substr($v, -1)){
            case 'h': $time_h = intval($v); break;
            case 'm': $time_m = intval($v); break;
            case 's': $time_s = intval($v); break;
        }
    }
    return $time_h . ':' . $time_m . ':' . $time_s;
}

//test array
$time[] = '1h 45s';
$time[] = '12h 35m 45s';
$time[] = '1m 45s';

// output
foreach($time as $t)
    var_dump(to_time($t));

This outputs

string '1:00:45' (length=7)
string '12:35:45' (length=8)
string '00:1:45' (length=7)
陈甜 2024-12-14 02:40:03
$string = "12h 34m 45s";
$string = str_replace(array("h","m","s"), array(":",":",""), str_replace(" ", "", $string));

echo $string;

像这样的东西吗?

$string = "12h 34m 45s";
$string = str_replace(array("h","m","s"), array(":",":",""), str_replace(" ", "", $string));

echo $string;

Something like this?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文