sorry for a noob question but I am wondering if you could format strings in php with comma or dashes, something like this:
Ex. #1
Sample Input: 123456789
Formatted Output: 123,456,789 or 123-456-789
Ex. #2
Sample Input: 0123456789
Formatted Output: 012,3456,789 or 012-3456-789
If anyone could help me out, that would be appreciated.
My Answer:
Yes, you can format integers or strings in PHP with commas or dashes using the number_format() function for integers and str_replace() function for strings.
For integers with commas:
php
$number = 1000000;
$formatted_number = number_format($number);
echo $formatted_number; // Output: 1,000,000
For strings with dashes:
php
$string = "hello world";
$formatted_string = str_replace(" ", "-", $string);
echo $formatted_string; // Output: hello-world
Rate this post
5 of 5 based on 6361 votesComments