PHP locale with money_format not working with fr_CA - php

I have this simple code:
<?php
setlocale('LC_MONETARY', 'fr_CA.UTF-8');
echo money_format('%+#10n', '-4562.75834');
print_r(localeconv());
?>
Who normally should give -4 562,76 $. But the result is always this:
(4 562,76 $)
The output of localeconv():
Array
(
[decimal_point] => .
[thousands_sep] =>
[int_curr_symbol] => CAD
[currency_symbol] => $
[mon_decimal_point] => ,
[mon_thousands_sep] =>
[positive_sign] =>
[negative_sign] => -
[int_frac_digits] => 2
[frac_digits] => 2
[p_cs_precedes] => 0
[p_sep_by_space] => 1
[n_cs_precedes] => 0
[n_sep_by_space] => 1
[p_sign_posn] => 1
[n_sign_posn] => 0
[grouping] => Array
(
)
[mon_grouping] => Array
(
[0] => 3
[1] => 3
)
)
We can see that negative sign should be - but not ()
The locale on the server (which is Debian 7), is installed:
# locale-gen
Generating locales (this might take a while)...
en_CA.UTF-8... done
fr_CA.ISO-8859-1... done
fr_CA.UTF-8... done
Generation complete.
I have tried with PHP 5.4.45 and 5.6.17

You have put the LC_MONETARY between single quotes.
It needs to live as a const variable which is set as not in quotes.
setlocale(LC_MONETARY, 'fr_CA.UTF-8');

Related

Parse ini file using PHP with symbol

Hi I need to parse the ini file shown below using php, which is basically created using Qt QSettings, I have tried the code here, but seems the INI file are different format there.
Is it possible to parse INI file in below format using PHP ?
[General]
Address=
Email=test#gmail.com
Password=123$
Sync=0
Loggedin=1
[Drives]
1\drive=/
1\free=281
1\size=908
2\drive=/media/New_Volume
2\free=6
2\size=7
3\drive=/media/sdb2
3\free=906
3\size=916
size=2
[Cam]
1\detection=0
1\Area.height=2.4
1\Area.width=9.5
2\detection=0
2\Area.height=2.4
2\Area.width=9.5
3\detection=0
3\Area.height=2.4
3\Area.width=9.5
size=2
Update:
Actually the above ini was working as said in below answer, and my actyual ini contains the $ symbol, that was creating the problem.
[General]
Address=
Email=test#gmail.com
Password=123$
Sync=0
Loggedin=1
[Drives]
1\drive=/
1\free=281
1\size=908
2\drive=/media/New_Volume
2\free=6
2\size=7
3\drive=/media/sdb2
3\free=906
3\size=916
size=2
[Cam]
1\detection=0
1\Area.height=2.4
1\Area.width=9.5
2\detection=0
2\Area.height=2.4
2\Area.width=9.5
3\detection=0
3\Area.height=2.4
3\Area.width=9.5
size=2
And seems I have to escape the $ symbol.
It works just fine, take a look at the php code while the .ini file stays exactly as you have it.
$ini_array = parse_ini_file("settings.ini",true);
print_r($ini_array);
//will output
Array
(
[General] => Array
(
[Address] =>
[Email] => test#gmail.com
[Password] => 123$ // note the $ sign in value here
[Sync] => 0
[Loggedin] => 1
)
[Drives] => Array
(
[1\drive] => /
[1\free] => 281
[1\size] => 908
[2\drive] => /media/New_Volume
[2\free] => 6
[2\size] => 7
[3\drive] => /media/sdb2
[3\free] => 906
[3\size] => 916
[size] => 2
)
[Cam] => Array
(
[1\detection] => 0
[1\Area.height] => 2.4
[1\Area.width] => 9.5
[2\detection] => 0
[2\Area.height] => 2.4
[2\Area.width] => 9.5
[3\detection] => 0
[3\Area.height] => 2.4
[3\Area.width] => 9.5
[size] => 2
)
)

Undefined Index for Valid key from array in PHP

While parsing a CSV file(I have used a Class found on Google), I ran into a problem. Here is an example of an array made from a .csv file(print_r):
Array
(
[0] => Array
(
[Site] => ViralNova
[Impressions] => 104719
[CTR] => 0.30%
[Clicks] => 311
[Average CPC] => $ 0.400
[CPM] => $ 1.19
[Conversion Rate] => 1.29%
[Actions] => 4
[CPA] => $ 31.100
[Spent] => $ 124.40
)
[1] => Array
(
[Site] => TMZ - Desktop
[Impressions] => 103276
[CTR] => 0.29%
[Clicks] => 295
[Average CPC] => $ 0.400
[CPM] => $ 1.14
[Conversion Rate] => 0.68%
[Actions] => 2
[CPA] => $ 59.000
[Spent] => $ 118.00
)
)
The Problem is that I cannot use the "Site" index. Whenever I try to, I get this notice:
<b>Notice</b>: Undefined index: Site in <b>C:\xampp\htdocs\panel\update\assets\php\core\core-ajax.php</b> on line <b>104</b><br />
Line 104 being:
print $data[0]['Site'];
Also,
print $data[0]['Impressions'];
or
print_r($data[0]);
works without any problem and I get the correct value/array.
Any ideas?
Thanks!!
Acc. to me your code is running fine.
this problem is occur when in loop in a point your index is missing.
to resolve this issue..
use this..
<?php
if($data[0]['Site']){
print $data[0]['Site'];
}
// Also
if($data[0]['Impressions']){
print print $data[0]['Impressions'];
}
?>

Extract item from an array

I want to be able to extract the output of [HostName] from this array in PHP. Thank you!
Array
(
[Protocol] => 17
[HostName] => [HG] JAILBREAK #1 - HeLLsGamers.com | 30+ LR's | FastDL
[Map] => ba_jail_hellsgamers_fx6
[ModDir] => cstrike
[ModDesc] => Counter-Strike: Source
[AppID] => 240
[Players] => 12
[MaxPlayers] => 64
[Bots] => 0
[Dedicated] => d
[Os] => l
[Password] =>
[Secure] => 1
[Version] => 1.0.0.75
[GamePort] => 27015
[ServerID] => -682722125
[GameTags] => HLstatsX:CE,alltalk,bunnyhopping,cool,drugs,free,gameme,gang,hellsgamers,hg,increased_maxplayers,jail,jailbr,nostats,startmoney
)
Like this:
echo $Array[HostName]; // output: [HG] JAILBREAK #1 - HeLLsGamers.com | 30+ LR's | FastDL
where $Array is the array you printed and copied here.
I recommend you start reading stuff from php.net - as this is the most basic thing you can do.
Thats the most simplest question .........
Just use this
echo $Your_array[HostName];

Using setlocale() doesn't affect PHPs number conversions

I have the following script:
<?php
$test = "2.5";
echo (float)$test;
echo "\n";
$r = setlocale(LC_ALL, "da_DK.UTF8");
setlocale(LC_ALL, NULL);
print_r(localeconv());
echo "\n";
echo (float)$test;
echo "\n";
echo (float)"2,5";
echo "\n";
?>
Which generates the following output:
2.5
Array
(
[decimal_point] => ,
[thousands_sep] => .
[int_curr_symbol] => DKK
[currency_symbol] => kr
[mon_decimal_point] => ,
[mon_thousands_sep] => .
[positive_sign] =>
[negative_sign] => -
[int_frac_digits] => 2
[frac_digits] => 2
[p_cs_precedes] => 1
[p_sep_by_space] => 2
[n_cs_precedes] => 1
[n_sep_by_space] => 2
[p_sign_posn] => 4
[n_sign_posn] => 4
[grouping] => Array
(
[0] => 3
[1] => 3
)
[mon_grouping] => Array
(
[0] => 3
[1] => 3
)
)
2,5
2
The very last line which reads 2 - I would have expected that to read 2,5 - and as far as I can see on the PHP documentation, it should.
If the second call to setlocale is omitted then the output of localeconv() is inconsistent with the danish locale - for reasons that are unclear to me.
(float)"2,5" equals 2 (note the comma) whereas (float)"2.5" equals 2.5. The reason can be read in the manual:
If the string does not contain any of the characters '.', 'e', or 'E' and the numeric value fits into integer type limits (as defined by PHP_INT_MAX), the string will be evaluated as an integer. In all other cases it will be evaluated as a float.
Type casting is not affected by setlocale().

Get currency ISO 4217 code based on locale

Say, if I parse HTTP Accept-Language header with Locale::acceptFromHttp is there an easy and reliable way to get user's preferred currency based on this locale identifier? Like "USD" for "en-US".
I wish there was a way to do this with PHP intl extension but so far was unable to find my answer in the manual. I saw Zend Framework can do this with Zend_Currency but it's just too bloated for my particular software.
Any other libs or ways of achieving this? Since there must be a lot of locale identifiers a simple switch is a bit of overkill.
You can do this in both PHP 4 and PHP 5 using setlocale() and localeconv():
$locale = Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']);
setlocale(LC_MONETARY, $locale);
print_r(localeconv());
Sample output:
Array
(
[decimal_point] => .
[thousands_sep] =>
[int_curr_symbol] => EUR
[currency_symbol] => €
[mon_decimal_point] => ,
[mon_thousands_sep] =>
[positive_sign] =>
[negative_sign] => -
[int_frac_digits] => 2
[frac_digits] => 2
[p_cs_precedes] => 1
[p_sep_by_space] => 1
[n_cs_precedes] => 1
[n_sep_by_space] => 1
[p_sign_posn] => 1
[n_sign_posn] => 2
[grouping] => Array
(
)
[mon_grouping] => Array
(
[0] => 3
[1] => 3
)
)
The ISO 4217 code is contained within the resulting array's int_curr_symbol key.
A bit late but you can get it with \NumberFormatter:
<?php
$currencyPerLocale = array_reduce(
\ResourceBundle::getLocales(''),
function (array $currencies, string $locale) {
$currencies[$locale] = \NumberFormatter::create(
$locale,
\NumberFormatter::CURRENCY
)->getTextAttribute(\NumberFormatter::CURRENCY_CODE);
return $currencies;
},
[]
);

Categories