压缩PHP代码

时间:2012-11-09 12:07:01

标签: php optimization

我已经创建了一个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++;
}

1 个答案:

答案 0 :(得分:3)

我会发布与posted in your previous thread相同的代码:

  

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

<?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日”等,因为该日期甚至不存在。

Codepad example of the code (WARNING: long output!)

修改

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

$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日?我知道我没有。

  等等等等等等等等等等?我知道我没有。