RE: [PHP] Leading zeroes
| From: | Ford, Mike [LSS] | Date: | Fri, 24 May 2002 10:02:54 +0000 |
| Subject: | RE: [PHP] Leading zeroes | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-99066@lists.php.net to get a copy of this message | ||
> -----Original Message-----
> From: Liam Gibbs [mailto:liamgibbs@yahoo.com]
> Sent: 23 May 2002 18:49
>
> > From version 4.1.0, $str = ltrim($str, '0')
>
> This works excellently. Thanks. One problem that I
> didn't think of, though: If the number is 0 (only 0),
> then the string ends up being empty. Is there a way
> around that? I put an if statement in saying if the
> string is empty, then the string is 0. Any better way?
Depends what you mean by better, but -- in addition to the other suggestions, what about:
if (strlen($str)>1):
$str = ltrim($str, '0');
endif;
This would save doing the ltrim() on all single-character strings, but I don't know if the cost
of the strlen() might offset this!
Cheers!
Mike
---------------------------------------------------------------------
Mike Ford, Electronic Information Services Adviser,
Learning Support Services, Learning & Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Beckett Park, LEEDS, LS6 3QS, United Kingdom
Email: m.ford@lmu.ac.uk
Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211