Forum Moderators: coopster

Message Too Old, No Replies

Simple query - Read 5 when var=005?

How to remove the 0 before the number

         

tomda

10:03 am on Sep 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi,

I call an include depending of GET['id'] variable.

Variable is checked with function but I have not yet found the solution to this.

When typing 005 instead of 5, everything works fine (because 005 and 5 are the same) except for calling the include (include005.php qnd include5.php are not the same).

What should I do to remove the zeros before any number?
So that 00000102 returns 102?

Thanks

Bonusbana

10:31 am on Sep 15, 2004 (gmt 0)

10+ Year Member



Something like:

function delZero($t)
{
$chars = strlen($t);
for ($i=1;$i<=$chars;$i++)
{
if (substr($t,$i,1)!= "0") break;
}
$return = substr($t,$i,($chars-$i));
return $return;
}

...should do it.

tomda

10:42 am on Sep 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hey Bonusbana,

Perfect!

Thanks a lot

dcrombie

3:54 pm on Sep 15, 2004 (gmt 0)



How about:

$val = $val - 0; # to convert to a number

or:

$val = intval($val); # to convert to an integer

IanKelley

4:09 am on Sep 16, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



In the spirit of scalability, use dcrombie's example.

If the script you're writing ever ends up having to go through 1000's of numbers you'll be glad you did :-)

Bonusbana

7:10 am on Sep 16, 2004 (gmt 0)

10+ Year Member



Agreed, use dcrombie's example if you just want to erase the zeros. The function I provided might be helpfull in other cases though, f.ex if you want to know how many zeros there are or if there is another number/digit you want to delete.

tomda

7:12 am on Sep 16, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thank for your support.

Indeed the second choice is much more shorter.