Turning a vulgar fraction into numbers in PHP

前端 未结 1 390
时光说笑
时光说笑 2021-01-22 23:53

How to turn a vulgar fraction into something PHP can use to compute the value of.

For example ¾

$l = \'¾\';

echo utf8_encode($l) . \' :: \' .ord($l) .          


        
1条回答
  •  说谎
    说谎 (楼主)
    2021-01-23 00:29

    You could use iconv() to transliterate the symbol into a form that can then be easily parsed:

    function utf8FractionToFloat($symbol)
    {
        $translit = trim(iconv('UTF-8', 'ASCII//TRANSLIT', $symbol));
    
        if ($translit && preg_match('~^(\d+)/(\d+)$~', $translit, $match)) {
            return $match[1] / $match[2];
        }
    }
    
    var_dump(utf8FractionToFloat('½')); // float(0.5)
    var_dump(utf8FractionToFloat('¾')); // float(0.75)
    

    0 讨论(0)
提交回复
热议问题