PHP String Exercises : Remove comma(s) from the specified numeric string
PHP String: Exercise-25 with Solution
Write a PHP script to remove comma(s) from the following numeric string.
Sample String : '2,543.12'
Visual Presentation:
Sample Solution:
PHP Code:
<?php
$str1 = "2,543.12";
// Define the original string.
$x = str_replace( ',', '', $str1);
// Remove all commas from the original string.
if( is_numeric($x))
// Check if the resulting string is numeric.
{
echo $x."\n";
// If numeric, echo the modified string.
}
?>
Sample Output:
2543.12
Explanation:
The above PHP code snippet removes all commas from the string "2,543.12" using the str_replace() function.
- str_replace(',', '', $str1) removes all occurrences of the comma , from the original string $str1, resulting in the string '2543.12'.
- The is_numeric() function checks if the resulting string $x is numeric.
- If $x is numeric, it echoes out the modified string '2543.12'.
So, the output of this code would be 2543.12.
Flowchart :
PHP Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a PHP script to select first 5 words from the specified string.
Next: Write a PHP script to print letters from 'a' to 'z'.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
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-25.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics