To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
PHPBuilder.com  
 

 

Go Back   PHPBuilder.com > PHP Help > Newbies

Newbies Help for those who are just getting started

Reply
 
Thread Tools Search this Thread Rate Thread Display Modes
Old 11-27-2005, 11:51 AM   #1
lozza1978
<?="help"?>
 
lozza1978's Avatar
 
Join Date: May 2005
Location: United-Kingdom Location: PC Location: Right here right now
Posts: 140
[RESOLVED] removing a leading zero

Hi all

for example I want to remove the leading 0 from this var
$var = 01

how would I do this ?
at the moment im using
ereg_replace("0", "", $test['2']);

but this deletes all the 0 zeros.

so

$var = 30;

becomes 3 which i dont want,

thanks all.

Last edited by lozza1978; 11-27-2005 at 11:59 AM.
lozza1978 is offline   Reply With Quote
Old 11-27-2005, 12:06 PM   #2
laserlight
PHP Witch
 
laserlight's Avatar
 
Join Date: Apr 2003
Location: Singapore
Posts: 13,058
Try using ltrim(), e.g.
PHP Code:
$var = ltrim($var, '0');
__________________
Use Bazaar for your version control system
Read the PHP Spellbook
Learn How To Ask Questions The Smart Way
laserlight is offline   Reply With Quote
Old 11-27-2005, 12:06 PM   #3
thorpe
addicted
 
thorpe's Avatar
 
Join Date: Aug 2004
Location: sydney
Posts: 3,457
PHP Code:
$var = "01";
echo
substr($var,1);
thorpe is offline   Reply With Quote
Old 11-27-2005, 03:31 PM   #4
lozza1978
<?="help"?>
 
lozza1978's Avatar
 
Join Date: May 2005
Location: United-Kingdom Location: PC Location: Right here right now
Posts: 140
Quote:
Originally Posted by laserlight
Try using ltrim(), e.g.
PHP Code:
$var = ltrim($var, '0');
thank you
lozza1978 is offline   Reply With Quote
Old 11-27-2005, 10:15 PM   #5
richie
Senior Member
 
Join Date: Aug 2002
Posts: 277
If you want all leading zeros removed:

$value = 003;

$value = intval($value);
__________________
http://www.nairaland.net/, http://www.oasisoflove.com/ - 100% FREE Online Dating
richie is offline   Reply With Quote
Old 07-26-2009, 12:06 PM   #6
danuel
Junior Member
 
Join Date: Jul 2009
Posts: 1
Quote:
Originally Posted by richie View Post
If you want all leading zeros removed:

$value = 003;

$value = intval($value);

It may be just as good slightly faster to use typecasting instead of function

$value = (int)$value; //typecasting instead of a function.

For a discussion on the topic, try searching google for "php intval vs int"

Also, if you're running a mission critical app, then remember php int limit is 2,147,483,647, and any integer above this will give you exactly 2,147,483,647. So, you may want to catch it by using something like:

if ($value > 2,147,483,646) {
//take any necessary steps, but don't continue with the script since
// chances are it will all be reported as 2,147,483,647
exit("Integer values larger then 2,147,483,646 are all reported as 2,147,483,647 and will be inaccurate");
}

Good luck.
danuel is offline   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT -4. The time now is 02:15 AM.








Acceptable Use Policy

Internet.com
The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.