Jump to content


Photo

Leading zero being removed


  • Please log in to reply
4 replies to this topic

#1 renno

renno
  • Members
  • PipPip
  • Member
  • 17 posts

Posted 19 October 2006 - 11:30 AM

When I upload a telephone number field to an SQL database, for example 01898543251, the leading zero is removed and becomes 1898543251. Is there a numeric type that keeps the leading zero or does it have to be inserted as a type of string?
Any help would be much appreciated...

Cheers,

#2 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 19 October 2006 - 11:35 AM

If your number is always 11 digits long then you can do this:

<?php
$number = 6848;
$length = 11;

if(strlen($number) < $length)
{
	for($i=0; $i<11-strlen($number); $i++)
	{
		$prepend .= "0";
	}
	$number = $prepend.$number;
}
echo $number;
?>


#3 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 19 October 2006 - 11:52 AM

I don't think there is a specific numeric datatype that will preserve leading zeros. Either store it as a string, or as an integer and restore the zeros when displying as in Daniel0s reply. You can also do it with str_pad()
<?php
echo str_pad ("1234",11,"0",STR_PAD_LEFT); //Displays 00000001234
?>


#4 renno

renno
  • Members
  • PipPip
  • Member
  • 17 posts

Posted 19 October 2006 - 11:54 AM

thanks loads, will give it a go...

#5 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 19 October 2006 - 11:56 AM

You can also do it with str_pad()

<?php
echo str_pad ("1234",11,"0",STR_PAD_LEFT); //Displays 00000001234
?>


Great, I must remember that trick. Much easier than my method.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users