Zend / PHP: How to remove all leading 0s from a string?

I have a string containing only numbers. Now I want to remove all leading 0s from this line

For instance:

input: 000000001230
output: 1230


input: 01000
output: 1000

Is there any function in PHP / Zend for this?

thank

+3
source share
4 answers
$myvar = ltrim('01000','0');
+18
source

No. There is only Zend_Filter_StringTrim, but it is not ltrim, but preg_replace(although unicode knows) from both ends. Or

use Zend_Filter_Callback:

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

or with filter instance

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

This way you can use it in a filter chain.

+6

int?

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

Zend_Filter_Int .

+3

Source: https://habr.com/ru/post/1741066/


All Articles