I'm drowning in PHP

Matt Wilson codebrewery at gmail.com
Tue May 15 08:31:57 BST 2007


On 5/15/07, Andy Armstrong <andy at hexten.net> wrote:
> This is a small gem from some PHP I'm obliged to look at:
>
>    function strip_zero($str) {
>      //Strips preceding zeros
>      $new = "";
>      $pre = 1;
>      for($i=0; $i<strlen($str); $i++) {
>        if($pre == 1) {
>          if($str{$i} != "0") {
>            $pre = 0;
>            $new .= $str{$i};
>          }
>        } else {
>          $new .= $str{$i};
>        }
>      }
>      return $new;
>    }
>
> Either written by a C programmer or naively converted from C I'd say.

Possibly. Or somebody who's just learning to code. I've seen much
worse during some of my PHP work though.

> Bear in mind that PHP /does/ have regex support.

$str = preg_replace ( $str, "/^0+/", "" );

IIRC, though the parameters may be in the wrong order :)


More information about the london.pm mailing list