Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trim any zeros at the beginning of a string using PHP

Tags:

php

trim

Users will be filling a field in with numbers relating to their account. Unfortunately, some users will have zeroes prefixed to the beginning of the number to make up a six digit number (e.g. 000123, 001234) and others won't (e.g. 123, 1234). I want to 'trim' the numbers from users that have been prefixed with zeros in front so if a user enters 000123, it will remove the zeroes to become 123.

I've had a look at trim and substr but I don't believe these will do the job?

like image 293
thisisready Avatar asked Sep 23 '10 20:09

thisisready


2 Answers

You can use ltrim() and pass the characters that should be removed as second parameter:

$input = ltrim($input, '0');
// 000123 -> 123

ltrim only removes the specified characters (default white space) from the beginning (left side) of the string.

like image 91
Felix Kling Avatar answered Oct 16 '22 21:10

Felix Kling


ltrim($usernumber, "0");

should do the job, according to the PHP Manual

like image 43
Tokk Avatar answered Oct 16 '22 20:10

Tokk