w3resource

PHP String Exercises : Remove all leading zeroes from a string

PHP String: Exercise-19 with Solution

Write a PHP script to remove all leading zeroes from a string.

Original String : '000547023.24'

Visual Presentation:

PHP String Exercises: Get the first word of a sentence

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 :

Flowchart: Remove all leading zeroes from a string

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.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/php-exercises/php-string-exercise-19.php