0

我创建了一个 PHP 脚本,但是在我的托管服务器中使用它时显示致命错误,然后我发现我需要压缩这个 PHP 代码才能使我的脚本工作。这是代码,任何人都可以用相同的输出,我是编码初学者,所以无法理解如何以最少的代码和努力来执行相同的输出,以便服务器可以轻松执行此代码。感谢代码需要优化:

$yar = 3;
while ($yar <= 9) {
    $ax = 31;
    while ($ax > 0) {
        $jaan = "$ax Jan 200$yar";
        $result = str_replace($jaan, " ", $result);
        $ax = $ax - 1;
    }
    $ax = 31;
    while ($ax > 0) {
        $feeb = "$ax Feb 200$yar";
        $result = str_replace($feeb, " ", $result);
        $ax = $ax - 1;
    }
    $ax = 31;
    while ($ax > 0) {
        $maar = "$ax Mar 200$yar";
        $result = str_replace($maar, " ", $result);
        $ax = $ax - 1;
    }
    $ax = 31;
    while ($ax > 0) {
        $appr = "$ax Apr 200$yar";
        $result = str_replace($appr, " ", $result);
        $ax = $ax - 1;
    }
    $ax = 31;
    while ($ax > 0) {
        $maay = "$ax May 200$yar";
        $result = str_replace($maay, " ", $result);
        $ax = $ax - 1;
    }
    $ax = 31;
    while ($ax > 0) {
        $juun = "$ax Jun 200$yar";
        $result = str_replace($juun, " ", $result);
        $ax = $ax - 1;
    }
    $ax = 31;
    while ($ax > 0) {
        $juul = "$ax Jul 200$yar";
        $result = str_replace($juul, " ", $result);
        $ax = $ax - 1;
    }
    $ax = 31;
    while ($ax > 0) {
        $auug = "$ax Aug 200$yar";
        $result = str_replace($auug, " ", $result);
        $ax = $ax - 1;
    }
    $ax = 31;
    while ($ax > 0) {
        $seep = "$ax Sep 200$yar";
        $result = str_replace($seep, " ", $result);
        $ax = $ax - 1;
    }
    $ax = 31;
    while ($ax > 0) {
        $occt = "$ax Oct 200$yar";
        $result = str_replace($occt, " ", $result);
        $ax = $ax - 1;
    }
    $ax = 31;
    while ($ax > 0) {
        $noov = "$ax Nov 200$yar";
        $result = str_replace($noov, " ", $result);
        $ax = $ax - 1;
    }
    $ax = 31;
    while ($ax > 0) {
        $deec = "$ax Dec 200$yar";
        $result = str_replace($deec, " ", $result);
    }
        $ax = $ax - 1;
    $yar++;
} //years start after 2010 to 2014
$yr = 10;
while ($yr <= 14) {
    $x = 31;
    while ($x > 0) {
        $jan = "$x Jan 20$yr";
        $result = str_replace($jan, " ", $result);
        $x = $x - 1;
    }
    $x = 31;
    while ($x > 0) {
        $feb = "$x Feb 20$yr";
        $result = str_replace($feb, " ", $result);
        $x = $x - 1;
    }
    $x = 31;
    while ($x > 0) {
        $mar = "$x Mar 20$yr";
        $result = str_replace($mar, " ", $result);
        $x = $x - 1;
    }
    $x = 31;
    while ($x > 0) {
        $apr = "$x Apr 20$yr";
        $result = str_replace($apr, " ", $result);
        $x = $x - 1;
    }
    $x = 31;
    while ($x > 0) {
        $may = "$x May 20$yr";
        $result = str_replace($may, " ", $result);
        $x = $x - 1;
    }
    $x = 31;
    while ($x > 0) {
        $jun = "$x Jun 20$yr";
        $result = str_replace($jun, " ", $result);
        $x = $x - 1;
    }
    $x = 31;
    while ($x > 0) {
        $jul = "$x Jul 20$yr";
        $result = str_replace($jul, " ", $result);
        $x = $x - 1;
    }
    $x = 31;
    while ($x > 0) {
        $aug = "$x Aug 20$yr";
        $result = str_replace($aug, " ", $result);
        $x = $x - 1;
    }
    $x = 31;
    while ($x > 0) {
        $sep = "$x Sep 20$yr";
        $result = str_replace($sep, " ", $result);
        $x = $x - 1;
    }
    $x = 31;
    while ($x > 0) {
        $oct = "$x Oct 20$yr";
        $result = str_replace($oct, " ", $result);
        $x = $x - 1;
    }
    $x = 31;
    while ($x > 0) {
        $nov = "$x Nov 20$yr";
        $result = str_replace($nov, " ", $result);
        $x = $x - 1;
    }
    $x = 31;
    while ($x > 0) {
        $dec = "$x Dec 20$yr";
        $result = str_replace($dec, " ", $result);
        $x = $x - 1;
    }
    $yr++;
}
4

1 回答 1

3

我将发布我在您之前的帖子中发布的相同代码:

当你可以做这样的事情时,为什么要经历如此漫长而奇怪的过程?

<?php
    $yearStart = 2004;
    $yearEnd = 2012;
    $unixTime = strtotime($yearStart . "-01-01 00:00:00");
    $endUnixTime = strtotime($yearEnd . "-12-31 23:59:59");
    while ($unixTime < $endUnixTime) {
        echo date("d M Y", $unixTime) . PHP_EOL;
        $unixTime = strtotime("+1 day", $unixTime);
    }
?>

输出:

01 Jan 2004
02 Jan 2004
03 Jan 2004
...
29 Dec 2012
30 Dec 2012
31 Dec 2012

这还具有不显示“2008 年 2 月 31 日”等的额外好处,因为该日期甚至不存在。

代码的键盘示例(警告:长输出!)

编辑

如果你想用空格替换这样的每个日期,你可以使用这个单行(用空格替换每个日期):

$result = preg_replace("/([0-2][0-9]|3[0-1]) (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) [0-9]{4}/", " ", $result);

会转

2004 年 1 月 1 日等等等等 2004 年 1 月 2 日和 2004 年 1 月 3 日等等,谁甚至在乎 2050 年 12 月 31 日?我知道我没有。

进入

等等等等等等,还有谁在乎呢?我知道我没有。

于 2012-11-09T12:10:41.363 回答