1

我已经构建了一个需要几秒钟的计算脚本,并在包含标签等的 y:d:h:m:s 中显示它们。

例如。15768012=1 year 12 seconds27362= 15 hours 6 minutes 2 seconds

该脚本运行良好,但我想查看字符串并将其除以最后一个数字(不是第一个数字),然后插入单词and,以便:

15768012=1 year AND 12 seconds27362= 15 hours 6 minutes AND 2 seconds

如果您需要,我可以编写我的脚本,但我认为它不会帮助您解决这个问题..

假设$string = '15 hours 6 minutes 2 seconds'您将如何拆分它并导入文本and

<?
if (isset($_POST['number'])) {
    $x = $_POST['number'];
} else {
    $x = 54098;
}

// Labels
$lb_y = 'year';
$lb_ys = 'years';
$lb_d = 'day';
$lb_ds = 'days';
$lb_h = 'hour';
$lb_hs = 'hours';
$lb_m = 'minute';
$lb_ms = 'minutes';
$lb_s = 'second';
$lb_ss = 'seconds';
$lb_and = 'and';

//$x = 54098; // Time in seconds - change to $row['time'];

$f = 15768000; // seconds in a year
$d = 43200; // seconds in a day
$t = 3600; // seconds in an hour
$m = 60; // senconds in a minute


// Let's check if it is more than a minute
if ($x >= $m) {

    // More than a minute
    // Let's check if it is more than an hour
    if ($x >= $t) {

        // More than an hour
        // Let's check if it is more than a day
        if($x >= $d) {

            // More than a day
            // Let's check if it is more than a year
            if ($x >= $f) {

                // More than a year
                // Calculate years
                $a = $x / $f;

                // Get everything before the separator '.'
                if (false !== ($cal = strpos($a, '.'))) {
                    $a = substr($a, 0, $cal);
                }

                // $k = what's left
                $k = $x - ($f * $a);

                // Calculate days
                $b = $k / $d;

                // Get everything before the separator '.'
                if (false !== ($cal = strpos($b, '.'))) {
                    $b = substr($b, 0, $cal);
                }

                // $y = what's left
                $y = $k - ($d * $b);

                // Calculate hours
                $c = $y / $t;

                // Get everything before the separator '.'
                if (false !== ($cal = strpos($c, '.'))) {
                    $c = substr($c, 0, $cal);
                }

                // $z = what's left
                $z = $y - ($t * $c);

                // Calculate minutes
                $e = $z / $m;

                // Get everything before the separator '.'
                if (false !== ($cal = strpos($e, '.'))) {
                    $e = substr($e, 0, $cal);
                }

                // $q = what's left
                $q = $z - ($m * $e);

                // Rewrite numbers if below 9
                if ($a <= 9) { $xa = '0'.$a; } else { $xa = $a; }
                if ($b <= 9) { $xb = '0'.$b; } else { $xb = $b; }
                if ($c <= 9) { $xc = '0'.$c; } else { $xc = $c; }
                if ($e <= 9) { $xe = '0'.$e; } else { $xe = $e; }
                if ($q <= 9) { $xq = '0'.$q; } else { $xq = $q; }

                // Rewrite labels
                if ($a <= 1) { $lb_ys = $lb_y; }
                if ($b <= 1) { $lb_ds = $lb_d; }
                if ($c <= 1) { $lb_hs = $lb_h; }
                if ($e <= 1) { $lb_ms = $lb_m; }
                if ($q <= 1) { $lb_ss = $lb_s; }

                // if == 0 - do not show
                $a = $a.' '.$lb_ys.' ';
                if ($b == 0) {$b = '';} else {$b = $b.' '.$lb_ds.' ';}
                if ($c == 0) {$c = '';} else {$c = $c.' '.$lb_hs.' ';}
                if ($e == 0) {$e = '';} else {$e = $e.' '.$lb_ms.' ';}
                if ($q == 0) {$q = '';} else {$q = $q.' '.$lb_ss;}

                echo $xa.':'.$xb.':'.$xc.':'.$xe.':'.$xq;
                echo '<br>'.$a.$b.$c.$e.$q;


            } else {

                // Less than a year - but more than one day
                // Calculate days
                $b = $x / $d;

                // Get everything before the separator '.'
                if (false !== ($cal = strpos($b, '.'))) {
                    $b = substr($b, 0, $cal);
                }

                // $y = what's left
                $y = $x - ($d * $b);

                // Calculate hours
                $c = $y / $t;

                // Get everything before the separator '.'
                if (false !== ($cal = strpos($c, '.'))) {
                    $c = substr($c, 0, $cal);
                }

                // $z = what's left
                $z = $y - ($t * $c);

                // Calculate minutes
                $e = $z / $m;

                // Get everything before the separator '.'
                if (false !== ($cal = strpos($e, '.'))) {
                    $e = substr($e, 0, $cal);
                }

                // $q = what's left
                $q = $z - ($m * $e);

                // Rewrite numbers if below 9
                if ($b <= 9) { $xb = '0'.$b; } else { $xb = $b; }
                if ($c <= 9) { $xc = '0'.$c; } else { $xc = $c; }
                if ($e <= 9) { $xe = '0'.$e; } else { $xe = $e; }
                if ($q <= 9) { $xq = '0'.$q; } else { $xq = $q; }

                // Rewrite labels
                if ($b <= 1) { $lb_ds = $lb_d; }
                if ($c <= 1) { $lb_hs = $lb_h; }
                if ($e <= 1) { $lb_ms = $lb_m; }
                if ($q <= 1) { $lb_ss = $lb_s; }

                // if == 0 - do not show
                $b = $b.' '.$lb_ds.' ';
                if ($c == 0) {$c = '';} else {$c = $c.' '.$lb_hs.' ';}
                if ($e == 0) {$e = '';} else {$e = $e.' '.$lb_ms.' ';}
                if ($q == 0) {$q = '';} else {$q = $q.' '.$lb_ss;}

                echo $xb.':'.$xc.':'.$xe.':'.$xq;
                echo '<br>'.$b.$c.$e.$q;
            }

        } else {

            // Less than a day
            // Calculate hours
            $c = $x / $t;

            // Get everything before the separator '.'
            if (false !== ($cal = strpos($c, '.'))) {
                $c = substr($c, 0, $cal);
            }

            // $z = what's left
            $z = $x - ($t * $c);

            // Calculate minutes
            $e = $z / $m;

            // Get everything before the separator '.'
            if (false !== ($cal = strpos($e, '.'))) {
                $e = substr($e, 0, $cal);
            }

            // $q = what's left
            $q = $z - ($m * $e);

            // Rewrite numbers if below 9
            if ($c <= 9) { $xc = '0'.$c; } else { $xc = $c; }
            if ($e <= 9) { $xe = '0'.$e; } else { $xe = $e; }
            if ($q <= 9) { $xq = '0'.$q; } else { $xq = $q; }

            // Rewrite labels
            if ($c <= 1) { $lb_hs = $lb_h; }
            if ($e <= 1) { $lb_ms = $lb_m; }
            if ($q <= 1) { $lb_ss = $lb_s; }

            // if == 0 - do not show
            $c = $c.' '.$lb_hs.' ';
            if ($e == 0) {$e = '';} else {$e = $e.' '.$lb_ms.' ';}
            if ($q == 0) {$q = '';} else {$q = $q.' '.$lb_ss;}

            echo $xc.':'.$xe.':'.$xq;
            echo '<br>'.$c.$e.$q;

        }

    } else {

        // Less than an hour
        // Calculate minutes
        $e = $x / $m;

        // Get everything before the separator '.'
        if (false !== ($cal = strpos($e, '.'))) {
            $e = substr($e, 0, $cal);
        }

        // $q = what's left
        $q = $x - ($m * $e);

        // Rewrite numbers if below 9
        if ($e <= 9) { $xe = '0'.$e; } else { $xe = $e; }
        if ($q <= 9) { $xq = '0'.$q; } else { $xq = $q; }

        // Rewrite labels
        if ($e <= 1) { $lb_ms = $lb_m; }
        if ($q <= 1) { $lb_ss = $lb_s; }

        // if == 0 - do not show
        $e = $e.' '.$lb_ms.' ';
        if ($q == 0) {$q = '';} else {$q = $q.' '.$lb_ss;}

        echo $xe.':'.$xq;
        echo '<br>'.$e.$q;

    }

} else {

    // Less than a minute
    // Only secounds

    // Rewrite numbers if below 9
    if ($x <= 9) { $xx = '0'.$x; } else { $xx = $x; }

    // Rewrite labels
    if ($x <= 1) { $lb_ss = $lb_s; }

    // if == 0 - do not show
    $x = $x.' '.$lb_ss;

    echo '00:'.$xx;
    echo '<br>'.$x;

}
?>

<form action="" method="post"><input name="number" type="text"><input name="" type="submit" value="Submit"></form>

谢谢大家的帮助..这是简化和完成的脚本..不过我有一个新问题..当您在该字段中输入少于时1576,它会写一些与我想要的不同的数字...

编辑:问题现已解决。欢迎使用脚本:D

<?
if (isset($_POST['number'])) {
    $x = $_POST['number'];
} else {
    $x = 54098;
}

// Labels
$lb_y = 'year';
$lb_ys = 'years';
$lb_d = 'day';
$lb_ds = 'days';
$lb_h = 'hour';
$lb_hs = 'hours';
$lb_m = 'minute';
$lb_ms = 'minutes';
$lb_s = 'second';
$lb_ss = 'seconds';
$lb_and = 'and';

//$x = 54098; // Time in seconds - change to $row['time'];

$f = 15768000; // seconds in a year
$d = 43200; // seconds in a day
$h = 3600; // seconds in an hour
$m = 60; // seconds in a minute

$a = $x / $f;
if (false !== ($cal = strpos($a, 'E-'))) { $a = 0; }
if (false !== ($cal = strpos($a, '.'))) { $a = substr($a, 0, $cal); }
if ($a <= 0) { $a = 0; }
$b = ($x - ($f * $a)) / $d;
if (false !== ($cal = strpos($b, 'E-'))) { $b = 0; }
if (false !== ($cal = strpos($b, '.'))) { $b = substr($b, 0, $cal); }
if ($b <= 0) { $b = 0; }
$c = ($x - ($f * $a) - ($d * $b)) / $h;
if (false !== ($cal = strpos($c, 'E-'))) { $c = 0; }
if (false !== ($cal = strpos($c, '.'))) { $c = substr($c, 0, $cal); }
if ($c <= 0) { $c = 0; }
$e = ($x - ($f * $a) - ($d * $b) - ($h * $c)) / $m;
if (false !== ($cal = strpos($e, '.'))) { $e = substr($e, 0, $cal); }
if ($e <= 0) { $e = 0; }
$q = ($x - ($f * $a) - ($d * $b) - ($h * $c) - ($m * $e));

// Rewrite numbers if below 9
if ($a <= 9) { $xa = '0'.$a; } else { $xa = $a; }
if ($b <= 9) { $xb = '0'.$b; } else { $xb = $b; }
if ($c <= 9) { $xc = '0'.$c; } else { $xc = $c; }
if ($e <= 9) { $xe = '0'.$e; } else { $xe = $e; }
if ($q <= 9) { $xq = '0'.$q; } else { $xq = $q; }

// Rewrite labels
if ($a <= 1) { $lb_ys = $lb_y; }
if ($b <= 1) { $lb_ds = $lb_d; }
if ($c <= 1) { $lb_hs = $lb_h; }
if ($e <= 1) { $lb_ms = $lb_m; }
if ($q <= 1) { $lb_ss = $lb_s; }

// if == 0 - do not show
if ($a == 0) {$a = '';} else {$a = $a.' '.$lb_ys;}
if ($b == 0) {$b = '';} else {$b = $b.' '.$lb_ds;}
if ($c == 0) {$c = '';} else {$c = $c.' '.$lb_hs;}
if ($e == 0) {$e = '';} else {$e = $e.' '.$lb_ms;}
if ($q == 0) {$q = '';} else {$q = $q.' '.$lb_ss;}

echo $xa.':'.$xb.':'.$xc.':'.$xe.':'.$xq.'<br>';

$time = array($a, $b, $c, $e, $q);

$time = array_filter($time);
$count = count($time);
$last = array_pop($time);
if ($count == 1) {
    $string = $last;
} elseif ($count == 0) {
    $string = '<i>No Time described</i>';
} else {
    $string = implode(', ', $time) . ' '.$lb_and.' ' . $last;
}

echo '<br>'.$string;

?>
<br><br>
<form action="" method="post"><input name="number" type="text"><input name="" type="submit" value="Submit"></form>
4

4 回答 4

2

快速修复可能是:

$string = '15 hours 6 minutes 2 seconds';
$pattern ='/ \d+ \w+$/';
$string = preg_replace($pattern, ' and$0', $string);

但是,您可能想要研究一个更好的解决方案,从而构建以下数组:

$time = array($a, $b, $c, $e, $q);

然后你可以这样做:

$time = array_filter($time);
$last = array_pop($time);
$string = implode(', ', $time) . ' and ' . $last;
于 2013-01-22T16:32:42.127 回答
2

当您可以使用除法来帮助计算出值时,这看起来相当冗长乏味。这样,您就可以一次对它们进行逻辑处理(以确定它们最后是否需要(s)。

$x / $f = $years
($x - ($f * $years)) / $d = $days
($x - ($f * $years) - ($d * $days)) / $h = $hours
($x - ($f * $years) - ($d * $days) - ($h * $hours)) / $m = $minutes
($x - ($f * $years) - ($d * $days) - ($h * $hours) - ($m * $minutes)) = $seconds

基本上,它会计算出之前每个计算的剩余部分还剩下多少。简单的例子是如果你的数字比 2 年大 1 秒。

年将给出 2,余数为 1。你减去你已经考虑的部分,然后继续分成更小的部分。这样,您可以更轻松地处理每个单独的数字以进行格式化。

于 2013-01-22T16:36:46.933 回答
0

由于您已经取得的成就相对复杂,我假设您只需要一些提示;所以:

  • explode功能: http: //php.net/manual/en/function.explode.php
  • 如您所知,您的字符串以数字及其单位结尾,请忽略由生成的数组的最后 2 个元素explode
  • 循环(以您认为合适的任何方式,不一定是传统for循环)通过数组并添加“和”。
于 2013-01-22T16:27:18.797 回答
0

如果你直接在你的函数中构建它会更有效,但同时你可以使用 preg_replace。

http://nl3.php.net/manual/en/function.preg-replace.php

$string = '15 hours 6 minutes 2 seconds'

$pattern ='/(\d+)\sseconds/';

$string =preg_replace($pattern,' And $1 seconds',$string);
于 2013-01-22T16:27:45.647 回答