Custom Number format and images in between
Question by Maarten Hartman
I’d like to format a number(stored as 10000.23, or 1.23) with php, , and place images between them, like the following.
10000,23
becomes
[img1]100[img2]00[img3]10
and
1,10
becomes
[img1]0[img2]1[img3]10
Anyone knows how to do this?
I tried several number formats but none of them allowed me to separate the last 4 digits into two pairs ( 00.00.00 )
Answer by Starx
Looks as if you want currency Format and replace the quotation marks with images. Here is a small function to add commas on the number.
function cFormat($number) {
while (true) {
$replaced = preg_replace('/(-?d+)(ddd)/', '$1,$2', $number);
if ($replaced != $number) {
$number = $replaced;
} else {
break;
}
}
return $number;
}
Then, here is how to replace them with images.
$int = 10000.23;
$int = cFormat($int);
// After adding comma, use str_replace() to replace comma with images
$int = str_replace(",", "<img src="comma.jpg" />", $int);
$int = str_replace(".", "<img src="period.jpg" />", $int);