0

I'm using this function to return a number which ends in .5 as a html ½ so 6.5 would become 6½ but for some reason 15.5 comes out as ½½, what should it be please?

function Fractionalise($value) {
 if ($value=="0.5") {
  $value = "½";
 }
 else {
  if (preg_match("/\.5[0]{0,}$/", $value)) $value = preg_replace("/.5[0]{0,}/", "½", $value);
 }
 return $value;
}
Andy Groom
  • 619
  • 1
  • 7
  • 15

2 Answers2

0

I think that in the preg_replace function you forgot an \.

It should be like

preg_replace("/\.5[0]{0,}/", "½", $value)
0

If you don't escape ., then it means "match any character". Small typo, but changes a lot.

You could simplify your code

function Fractionalise($value) {
  return preg_replace("/\.50*/", "½", $value);
}
An0num0us
  • 961
  • 7
  • 15