0

I have created a PHP script but while using it in my hosting server it show fatal error, then i have discovered that i need to compress this PHP code to make my script working.. Here is the code, any one can compress this code with the same output, i am a beginner in coding so can't understand how to act this same output with minimized code and efforts so that server can easily execute this code. Thanks The Code Need to be optimize:

$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
  • It's completely unclear why "compression" would help you. How did you discover this? Talking about optimization, what are you doing in your script anyways? A little explanation is likely to result in helpful suggestions of how to do it. Commented Nov 9, 2012 at 12:11

1 Answer 1

3

I'll post the same code that I posted in your previous thread:

Why go through such a long and odd process, when you can do something like this?

<?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);
    }
?>

Output:

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

This also has the added bonus of not showing "31 Feb 2008" etc., as that date doesn't even exist.

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

Edit

If you want to replace every date like this with a space, you can use this one-liner (replace every date with a space):

$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);

It will turn

01 Jan 2004 blah blah 02 Jan 2004 and blah 03 Jan 2004 and who even cares about 31 Dec 2050? I know that I don't.

into

blah blah and blah and who even cares about ? I know that I don't.

Sign up to request clarification or add additional context in comments.

8 Comments

but if you have a look again in the code, i need to use str_replace for every date to replace those date by a space. I will use this in simple html php dom parser
@MaxMuller Then instead of echo date("d M Y", $unixTime) . PHP_EOL; use $result = str_replace(date("d M Y", $unixTime), " ", $result);.
@MaxMuller Optionally, check my edit. It might be way more of what you need.
one more question, is this will also check 1 Jan 2004 not 01 Jan 2004 etc ... ?
@MaxMuller No - it won't, but then you can change the first part of the regex to this: ([1-9]|[0-2][0-9]|3[0-1]) instead of ([0-2][0-9]|3[0-1]).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.