giles
giles

Reputation:

Removing leading 0?

Is there a simple way to remove a leading zero (as in 01 becoming 1)?

Upvotes: 2

Views: 3643

Answers (5)

Matt
Matt

Reputation: 1281

You can use the ltrim function:

ltrim($str,"0");

Upvotes: 21

annakata
annakata

Reputation: 75794

Regex replace /^0*/ with '' for a string return solution

The exact code will be something like this

<?php
 $string_number = '000304';
 echo preg_replace('/^0*/', '', $string_number);
?>

Upvotes: 0

farzad
farzad

Reputation: 8855

if you use the trim functions, you might mistakenly remove some other character, like by triming "12" your will have "2". use the intval() function. this function will convert your string (which could start by a leading zero or not) to an integer value. intval("02") will be 2 and intval ("32") wll be 32.

Upvotes: 4

J.D. Fitz.Gerald
J.D. Fitz.Gerald

Reputation: 2957

Just multiply by 1

echo "01"*1

Upvotes: -1

Tomalak
Tomalak

Reputation: 338158

$str = "01";
echo intval($str);

Upvotes: 8

Related Questions