views:

77

answers:

2

I have a string with many fractions like 1/2, 1/4 etc. I want to replace them with their Unicode equivalents.

I realise I could pick them up with

/\s(\d+)\/(\d+)\s/

How would I replace them with their Unicode equivalents? I could probably wrap the numbers in span and do something similar with CSS, but I was wondering if there was an easy way to convert them.

Do I need to have a 1:1 mapping of regex to Unicode character?

Thanks

+4  A: 

Given that there's only a few of them, you could just create a mapping like so:

$fractions = array(
    '1/4' => '¼', '1/3' => '⅓',
    '3/8' => '⅜', ...
  );

I'd say that's probably the easiest way... kind in mind that many people won't have fonts installed that can display all of them anyway.

Dean Harding
I think this is probably the best way, thanks Dean.
alex
@Dean: I wonder if there's any statistical data on how many people don't have decent Unicode support.
Tadeusz A. Kadłubowski
Fractions are part of the "extended ASCII" set, so 99% of people will have (at least) ½, ¼, and ¾ available. Honestly though, I would probably go for the solution below with `<sup>` and `<sub>`. That way you don't have to worry about the Unicode conversion (and possibly get it wrong) and you are not limited by what characters are available, eg: any fraction is possible, not just the ones that Unicode covers.
shadowhand
There is no such thing as a single “extended ASCII” character set. You're probably thinking of ISO-8859-1.
bobince
@Tadeusz: Most people have environments that "support" Unicode, the problem is there may not be support in all (many?) fonts for some of the more "esoteric" fractions. ½, ¼, ¾ sure, but there's also ones such as ⅞ which they might not have.
Dean Harding
+6  A: 

You'll have better luck displaying it this way. Not everyone may have those Unicode fonts you need.

echo preg_replace ( '/\b(\d+)\/(\d+)\b/', '<sup>$1</sup>/<sub>$2</sub>', $fraction );

or:

echo preg_replace ( '/\b(\d+)\/(\d+)\b/', '<sup>$1</sup>&#8260;<sub>$2</sub>', $fraction );

The fractional slash &#8260; will look like ⁄.

This is still the best solution if you need to display large fractions: 25675798

stillstanding
I'm going to try it with the Unicode chars, and if my mileage is poor, I'll check out this solution. Thanks for posting, +1
alex