Formatting of coins in PHP

Asked

Viewed 347 times

-3

Greetings to all I am working on a system in PHP, and I would like to know if there is a function that makes PHP recognize or use the currency that is defined in windows!

  • In windows? Wouldn’t it be better defined by the browser?

  • How would you define it so

  • How so currency defined in Windows? Does it refer to detect the person’s country and configure the currency exchange and type? I could explain better what you need so we can guide you?

1 answer

0


When your browser requests a web page, it sends a language acceptance header ACCEPT_LANGUAGE that tells you which languages you can accept the content and in what order.

Examples of strings returned by header HTTP_ACCEPT_LANGUAGE

  • es-ES,es;q=0.8,en-US;q=0.5,en;q=0.3
  • en-US,en;q=0.8,pt-BR;q=0.6,pt;q=0.4

Mine on this machine is:

  • pt-BR,pt;q=0.8,en-US;q=0.6,en;q=0.4

that is to say, I prefer the Brazilian Portuguese, if it doesn’t have it can be "general" Portuguese (weight 0.8). In the absence of these, I can accept US English (q = 0.6), and finally "general" English (weight 0.4)

The parameter q (quality factor), indicates user preference order.

With this string we can make PHP use the currency based on this information.

/*idioma do navegador do Usuário (o primeiro da lista que tem a mais alta preferencia 
e tem formatação próxima a necessária para uso em setlocale).
entre as aspas simples pode colocar uma default (exemplo: pt-BR ou en-US)
*/
$http_lang = isset($_SERVER["HTTP_ACCEPT_LANGUAGE"]) ? substr($_SERVER["HTTP_ACCEPT_LANGUAGE"],0,5) : '';

//formata para valor válido em setlocale
//substitui o "-" (tracinho) por "_" (underline)
$http_lang = str_replace("-","_",$http_lang);

//Define informações locais
setlocale(LC_ALL, $http_lang);

//retorna dados baseados na localidade corrente definida por setlocale().
$locale_info = localeconv();

//Simbolo da moeda local
$simbolo = $locale_info['currency_symbol'];
//caractere decimal
$decimal_point = $locale_info['decimal_point'];
//Separador de Milhares 
$thousands = $locale_info['thousands_sep'];

Example of use:

$valor = 12345678900;

echo $simbolo.number_format($valor,2,$decimal_point,$thousands);

see result in ideone

Most browsers have settings that allow you to check or change language preference settings.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.