From: Ævar Arnfjörð Bjarmason Date: Fri, 8 Apr 2005 19:56:09 +0000 (+0000) Subject: * Adding a new function to format a number like 1520626 as 1,520,626 and X-Git-Tag: 1.5.0alpha1~340 X-Git-Url: http://git.cyclocoop.org/%24action?a=commitdiff_plain;h=7e420ce5991f3815df0d4d9c2340d5e7dc990c3b;p=lhc%2Fweb%2Fwiklou.git * Adding a new function to format a number like 1520626 as 1,520,626 and 1520626.34 as 1,520,626.34 etc, using it for MAG_NUMBEROFARTICLES. --- diff --git a/includes/Parser.php b/includes/Parser.php index 0c3d83398b..1f3eb330a8 100644 --- a/includes/Parser.php +++ b/includes/Parser.php @@ -1725,7 +1725,7 @@ class Parser case MAG_CURRENTDOW: return $varCache[$index] = $wgContLang->formatNum( date('w') ); case MAG_NUMBEROFARTICLES: - return $varCache[$index] = $wgContLang->formatNum( wfNumberOfArticles() ); + return $varCache[$index] = $wgContLang->formatNum( commafyNum( wfNumberOfArticles() ) ); case MAG_SITENAME: return $wgSitename; case MAG_SERVER: @@ -3051,6 +3051,40 @@ function &outputReplaceMatches( $matches ) { return $outputReplace[$matches[1]]; } +/** + * Formats a number like 1520626 as 1,520,626 and 1520626.34 as 1,520,626.34 + * + * @todo It's possible to do this with a regex, see perlfaq5 + * + * @param mixed Numbers in the form of integers, floats or strings. + * @return string + */ +function commafyNum( $input ) { + if ( preg_match('/^\d+$/', $input) ) { + $intpart =& $input; + } elseif ( preg_match('/^\d+\.\d+$/', $input) ) { + list($intpart, $floatpart) = explode( '.', $input); + } else { + wfDebug("Error: Invalid input given to seperateNumber()\n"); + return; + } + + $chars = preg_split( '//', $intpart, -1, PREG_SPLIT_NO_EMPTY ); + + $len = count($chars); + for($i = 0, $nr = 0; $i < $len; ++$i ) { + if ($nr++ % 3 === 0 && $i !== 0) + $t[] = ','; + $t[] = array_pop($chars); + } + + $str = implode( null, array_reverse($t) ); + + if ( isset($floatpart) ) + $str .= '.' . $floatpart; + return $str; +} + /** * Return the total number of articles */