views:

117

answers:

4

I have a string which contains only numbers. Now I want to remove all leading 0s from that string

For example:

input: 000000001230
output: 1230


input: 01000
output: 1000

Is there any function in PHP/Zend for this?

Thanks

+17  A: 
$myvar = ltrim('01000','0');
oezi
+5  A: 

No. There is only Zend_Filter_StringTrim, but that does not ltrim, but preg_replace (unicode aware though) from both ends. Either

use Zend_Filter_Callback:

echo Zend_Filter::filterStatic('000111000', 'Callback', array('ltrim', '0'));
// gives 111000

or with a filter instance

$trimmer = new Zend_Filter_Callback('ltrim', '0');
echo $trimmer->filter('000111000'); // gives 111000

This way you could use it in a Filter chain.

Gordon
+3  A: 

Zend_Filter_Int will also work for this particular case.

Rob Allen
+3  A: 

Can't you just cast to int?

$s = '00000414';
print (int)$s; // 414
Inkspeak