Support » Plugins » Better zeroise() function.

  • Here’s the zeroise() function SPAM is currently using:
    function zeroise($number, $threshold) { // function to add leading zeros when necessary
    $l=strlen($number);
    if ($l<$threshold)
    for ($i=0; $i<($threshold-$l); $i=$i+1) { $number='0'.$number; }
    return $number;
    }

    I wrote this little piece of code, which should do the same, but is much smaller and faster.
    function zeroise($number, $treshold) {
    return sprintf("%0".$treshold."s", $number);
    }

    Good? Bad? Let me know.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator Matt Mullenweg

    (@matt)

    Troublemaker

    This seems to break month navigation.

    Can you please specify, Matt? It seems to return exactly the same as with the old function to me…

    Moderator Matt Mullenweg

    (@matt)

    Troublemaker

    Sure, http://fresh.wordpress.org/
    That’s a CVS version with the only change being your function, note that the months aren’t getting padded and the calendar names and links are messed up as a result.

    I’ve been looking into that for 4 days now, and I still can’t find the problem. I implemented this on my blog (which is running the official 1.2 Mingus release), and everything works fine. I also freshly installed WP 1.2 Mingus on my localhost, with the only change being the function, and everything works like a charm.
    Somebody, please enlighten me here! Are there such great changes in the CVS version or something?

    How about:
    str_pad($number, $threshold, ‘0’, STR_PAD_LEFT)
    (Built into PHP >= 4.0.1.; see <http://www.php.net/str_pad&gt;).

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Better zeroise() function.’ is closed to new replies.