Re: implode() in MDB_QueryTool - decimal-point -> comma
| From: | Lukas Ruetz | Date: | Mon, 15 May 2006 15:48:37 +0000 |
| Subject: | Re: implode() in MDB_QueryTool - decimal-point -> comma | ||
| References: | 1 2 3 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-24080@lists.php.net to get a copy of this message | ||
Am Montag, 15. Mai 2006 16:43 schrieb Lorenzo Alberton:
> Hi Lukas,
>
> > implode() converts each value to a string and thats the point where the
> > locale is importand. Finally when I looked at setlocale() I found what I
> > was searching for - a setting for the decimal-char (locale). It was too
> > easy ..
> >
> > @Lorenzo - so a standard western-europe locale (without setting the
> > LC_NUMERIC option) stops QueryTool from working correctly. Maybe this
> > could be addressed in a future release.
>
> I think I now understand what you mean.
> There's a bug report for PEAR::DB on this very issue:
>
> http://pear.php.net/bugs/bug.php?id=3021
>
> Quite frankly, I don't know how this could be solved
> without requiring expensive metadata calls within each
> add() call, and to quote the values one by one (vs. the
> current _quoteArray() approach) and calling number_format()
> in case of a float column.
> I'm not sure how to circumvent this behaviour,
> since inlining the string would not be different
> from imploding the array:
>
> $ php -r 'setlocale(LC_ALL, "de_DE"); echo "". 1.5 ."\n";
>
> Do you have any better solution?
> And btw, what's your setup (dbms type & version, php version)?
Hi Lorenzo!
I'm using PHP 4.4.0 and Postgres 8.0.3
I see only the two solutions like they have been stated in the bug-report
http://pear.php.net/bugs/bug.php?id=3021 form
others.
1) to have an "own" implode that checks the type of each value.
function my_implode( $concatStr, $array ) {
$str = '';
foreach( $array as $value ) {
if( $str !== '' ) $str .= $concatStr;
$str .= is_double($value) ? floatToStringNoLocale($value) : $value;
}
return $str;
}
// author [glideraerobatics at hotmail dot com]
function floatToStringNoLocale($float) {
$precision = strlen($float) - strlen(intval($float));
if ($precision) {
$precision--; // don't count decimal seperator
}
return number_format($float, $precision, '.', '');
}
simply replacing the comma by a dot is a problem for locales that use
another char.
2) to change the locale in add(). That is definitly _not_ a nice solution.
Problem: Is en_US (or C?) always available?
// author [php at ter dot dk]
$original_locale = setlocale(LC_NUMERIC,0);
setlocale(LC_NUMERIC, 'en_US');
.. execute stuff ..
setlocale(LC_NUMERIC, $original_locale);
I tend to use the first solution. I'm programming too less with PHP to see
a faster implementation for now.
greetz
lukas