PHP String Exercises : Remove all leading zeroes from a string
Write a PHP script to remove all leading zeroes from a string.
Original String : '000547023.24'
Visual Presentation:
Sample Solution:
PHP Code:
<?php
$x = '000547023.24';
// Define the original string.
$str1 = ltrim($x, '0');
// Remove leading zeros from the string.
echo $str1."\n";
// Output the modified string.
?>
Output:
547023.24
Explanation:
The above PHP code snippet takes the string '000547023.24' and removes any leading zeros from it using the "ltrim()" function. The "ltrim()" function trims characters from the left side of a string. Here, it trims leading zeros from the string '000547023.24', resulting in the string '547023.24'. Finally, it echoes out the modified string.
Flowchart :
PHP Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a PHP script to get the first word of a sentence.
Next: Write a PHP script to remove part of a string.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics