PHP remove first zeros

PHP remove first zeros

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

PHP remove first zeros

Handling numeric strings in PHP often reveals subtle distinctions between how data is stored versus how it is interpreted. A common requirement involves stripping leading zeros from a string representation of a number while preserving the integer's value and ensuring trailing zeros are not affected. The goal is to transform inputs like 0002 into 2, 0203410 into 203410, and leave 20000 entirely unchanged.

The most straightforward approach relies on PHP's type coercion, which naturally handles the interpretation of numeric strings. When you cast a string to an integer using (int) or (integer), PHP parses the value according to standard arithmetic rules: leading zeros are ignored for the purpose of the numerical value.

$var = '0002';
echo (int)$var; // Outputs: 2

$var = '0203410';
echo (int)$var; // Outputs: 203410

$var = '20000';
echo (int)$var; // Outputs: 20000

This method is efficient and concise. However, the resulting value is an integer. If your application requires the result to remain a string—for instance, if you need to concatenate it later or maintain specific string formatting properties—you cannot simply cast it back to a string without risk. In PHP, casting an integer back to a string via (string) yields the canonical representation of that number, which effectively removes the leading zeros but also normalizes the type.

$result = (string)(int)'0002';
var_dump($result); // string(1) "2"

While this achieves the desired visual result, it alters the data type from string to integer and back. This is generally acceptable if the original input was merely a placeholder for a number, but it can be problematic if the variable was intended to hold arbitrary alphanumeric sequences that happen to start with zeros (though such cases are rare in strictly numeric contexts).

If you need to manipulate the string directly without changing its type, regular expressions offer a precise alternative. The pattern ^0+ matches one or more zero characters at the very beginning of the string. You can use preg_replace to replace this specific sequence with an empty string.

function stripLeadingZeros($var) {
    return preg_replace('/^0+(?=\d)/', '', $var);
}

$var = '0002';
echo stripLeadingZeros($var); // Outputs: 2

$var = '0203410';
echo stripLeadingZeros($var); // Outputs: 203410

$var = '20000';
echo stripLeadingZeros($var); // Outputs: 20000

The lookahead assertion (?=\d) is crucial here. It ensures that the zeros are only removed if they are followed by at least one digit. This prevents edge cases where a string consists entirely of zeros (e.g., '000'), which would otherwise become an empty string after replacement. While the numeric interpretation of all zeros is 0, keeping it as an empty string might break logic expecting a number. If input validation guarantees that non-zero values exist, you can simplify the regex to just ^0+.

Another consideration involves floating-point numbers. Casting a float like '0002.5' to an integer truncates the decimal portion, yielding 2. If preserving decimals is necessary, the regular expression approach remains superior as it operates strictly on the character sequence without type coercion side effects.

In modern PHP development frameworks like Laravel, input sanitization often happens automatically via model casting or mass assignment rules. However, when dealing with raw form inputs that might contain leading zeros (common in ID fields or legacy data imports), applying this logic explicitly before storage is a prudent defensive measure. Whether you use simple type casting for performance-critical paths where string semantics are irrelevant, or regex for precise string manipulation, the choice depends on whether the downstream system expects an integer or a normalized string.

Our network