Friday, November 14, 2025
HomeLanguagesHow to remove all leading zeros in a string in PHP ?

How to remove all leading zeros in a string in PHP ?

Given a number in string format and the task is to remove all leading zeros from the given string in PHP.

Examples:

Input : str = 00006665555
Output : 6665555

Input : str = 00775505
Output : 775505

Method 1: Using ltrim() function: The ltrim() function is used to remove whitespaces or other characters (if specified) from the left side of a string.

Syntax:

ltrim( "string", "character which to be remove from the left side of string");

Program:




<?php
  
// Store the number string with
// leading zeros into variable
$str = "00775505";
  
// Passing the string as first
// argument and the character
// to be removed as second
// parameter
$str = ltrim($str, "0"); 
          
// Display the result
echo $str;
  
?>


Output:

775505

Method 2: First convert the given string into number typecast the string to int which will automatically remove all the leading zeros and then again typecast it to string to make it string again.

Program:




<?php
  
// Store the number string with
// leading zeros into variable
$str = "00775505";
  
// First typecast to int and 
// then to string
$str = (string)((int)($str));
          
// Display the result
echo $str;
  
?>


Output:

775505
RELATED ARTICLES

Most Popular

Dominic
32399 POSTS0 COMMENTS
Milvus
95 POSTS0 COMMENTS
Nango Kala
6765 POSTS0 COMMENTS
Nicole Veronica
11917 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11984 POSTS0 COMMENTS
Shaida Kate Naidoo
6889 POSTS0 COMMENTS
Ted Musemwa
7142 POSTS0 COMMENTS
Thapelo Manthata
6837 POSTS0 COMMENTS
Umr Jansen
6840 POSTS0 COMMENTS