PHP在最后一个数字处分割字符串,插入一个额外的字符串并合并新的字符串 [英] PHP split string at last number, insert an extra string and merge the new string

查看:67
本文介绍了PHP在最后一个数字处分割字符串,插入一个额外的字符串并合并新的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have build a calculating script that takes seconds and show them in y:d:h:m:s containing labels etc.

例如 15768012 = 1年12秒 27362 = 15小时6分2秒.

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

The script works perfectly, but I would like to look in the string and split it by the last number (not the first) and then insert the word and so that:

15768012 = 1年12秒钟 27362 = 15小时6分钟2秒.

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

I can write my script if you need it, but I don't think it will help you solve this question..

让我们说 $ string = '15小时6分2秒'您将如何拆分它并导入文本 and ?

let's say that $string = '15 hours 6 minutes 2 seconds' how would you split it and import the text 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 时,它会写一些与我想要...

Thanks everybody for your help.. here is the simplified and done script.. I have a new problem though.. when you type less than 1576 in the field, it writes some different numbers than I want it to...

问题现已解决.欢迎使用脚本:D

Problem now fixed.. Welcome to use the script :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>

推荐答案

一个快速修复方法可能是:

A quick fix may be:

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

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

However, you may want to look into a nicer solution which resulted in the following array being built:

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

然后您可以这样做:

$time = array_filter($time);
$last = array_pop($time);
$string = implode(', ', $time) . ' and ' . $last;

这篇关于PHP在最后一个数字处分割字符串,插入一个额外的字符串并合并新的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆