2017-04-02 15 views
0

많은 연구 후에합니다 .PHP의 형식화하는 번호는 실제로 사용자가 잘 살고 로케일에 대한 형식 번호에 다음과 같은 기능을 사용하고 로케일 다시

그러나 사용자가 숫자를 입력 할 수있는 양식이있는 경우 DB에 저장할 수 있도록 서식을 다시 지정하는 기능이 필요합니다. 이 솔루션을 찾았습니다.

function number_format_en($number) { 
    // $_SESSION['lang']['locale'] has the locale like de_DE, ar_AE, tr_TR, ... 
    $fmt = numfmt_create($_SESSION['lang']['locale'], NumberFormatter::DECIMAL); 
    return numfmt_parse($fmt, $number); 
} 

이 테스트는 내가 테스트 한 대부분의 로캘에서 작동하지만 모든 사용자와 함께 작동하지 않습니다! 예를 들어 : 나는 (ar_AE) 아랍어 사용하는 경우 :

$randomNumber = 3171003633.95; 
$number2locale = number_format_i18n($randomNumber, 2); 
// works as expected: 3,171,003,633.95 
// now format it back: 
$locale2number = number_format_en($number2locale); 
// here I get this: 3.171 

가 어떻게 "다시"EN-형식으로 안전한 방법으로 번호를 입력 로케일을 포맷 할 수 있습니다? 아니면 숫자 형식의 모든 종류를 감지하고 en-format 형식으로 DB에 저장할 수있는 방법이 있습니까?

답변

0

그래서,이 좋은 해결책인지 아닌지 정말 모르겠어요 -하지만 잘 동작하는 것 :

function number_format_en($number) { 
    // first remove everything execpt -,. 
    $cleanNumber = preg_replace('/[^\\d-,.]+/', '', $number); 

    $last_dot = strrpos($cleanNumber, '.'); 
    $last_comma = strrpos($cleanNumber, ','); 

    if($last_dot !== false || $last_comma !== false) { 
     if($last_dot > $last_comma) { // decimal seperator = dot 
      $decimal_point = '.'; 
      if(substr_count($cleanNumber, '.') > 1) { 
       // could be totaly wrong 1,234.567.890 
       // or there are no decimals 1.234.567.890 
       // removing all dots and commas and returning the value 
       return preg_replace('/[^\\d-]+/', '', $cleanNumber); 
      } 
     } else { // decimal seperator = comma 
      $decimal_point = ','; 
      if(substr_count($cleanNumber, ',') > 1) { 
       // could be totaly wrong 1.234,567,890 
       // or there are no decimals 1,234,567,890 
       // removing all dots and commas and returning the value 
       return preg_replace('/[^\\d-]+/', '', $cleanNumber); 
      } 
     } 
    } else { // no decimals 
     $decimal_point = false; 
     $decimals = 0; 
    } 

    if($decimal_point !== false) { 
     // if decimals are delivered, get the count of them 
     $length = strlen($cleanNumber); 
     $position = strpos($cleanNumber, $decimal_point); 
     $decimals = $length - $position - 1; 

     if($decimal_point == '.') { 
      // remove all commas if seperator = . 
      $cleanNumber = str_replace(',', '', $cleanNumber); 
     } elseif($decimal_point == ',') { 
      // remove all dots if seperator = , 
      $cleanNumber = str_replace('.', '', $cleanNumber); 
      // now switch comma with dot 
      $cleanNumber = str_replace(',', '.', $cleanNumber); 
     } 
    } 
    return $cleanNumber; 
} 

내가 마지막 점이나 쉼표를 확인거야, 그리고 그 I 형식에 따라 숫자 함수는 원래 숫자와 같은 소수 자릿수를 반환합니다. 는 "알려진"버그가 있습니다 : 누군가가이 경우에 코멘트를 게시 할 수있는 경우

number_format_en('1.234.567,89') // 1234567.89 
number_format_en('-1,234,567.89') // -1234567.89 
number_format_en('1.234.567.89') // 123456789 (only dots) 
number_format_en('1,234,567,89') // 123456789 (only commas) 

좋을 텐데 : 사람이 1.000.000 또는 100처럼 전체 번호를 입력하는 경우는 예를 들어 1000000

을 반환 좋은 방법인지 아닌지 또는 더 나은 해결책을 제시하는 답변입니다.