Andrew Johnson
Andrew Johnson

Reputation: 51

remove leading zeros php skip leading string

I am very new to PHP.

I have fields returning from a database that are our stock numbers.

e.g. C000001204 and also GC00001204

I hope to remove the leading zeros but keep the prefix of C and GC. I do not wish to remove the 0 in between the 2 and the 4.

I had a good search for this however I could only find solutions that ltrim the start.

Thanks in advance!

Andy

Upvotes: 1

Views: 420

Answers (1)

Paul
Paul

Reputation: 141839

Regex (preg_replace) would work well here:

$str = 'C000000001204';
$str = preg_replace('/^(\D*)0*/', '$1', $str);

Demo

Upvotes: 2

Related Questions