Page MenuHomePhabricator
Authored By
bzimport
Nov 21 2014, 10:07 PM
Size
1 KB
Referenced Files
None
Subscribers
None

test.php

<?php
function wiki_number_format( $value ) {
preg_match( '/
(?P<whole>\d?[\d,]*)\.?(?P<decimal>\d?[\d ]*)\s*
(?:\((?P<uncertainty>\d+)\))?\s*
(?:\*(?P<base>\d+)\^(?P<exponent>\d+))?\s*
(?P<unit>\w*)
/x', $value, $matches );
# Cleanup if needed
$matches['whole'] = str_replace( ',', '', $matches['whole'] );
$matches['decimal'] = str_replace( ' ', '', $matches['decimal'] );
# Chopping up
$whole = str_split( $matches['whole'], 3 );
$matches['whole'] = implode( ',', $whole );
$decimal = str_split( $matches['decimal'], 3 );
if( count( $decimal ) >= 2 && strlen ( end( $decimal ) ) == 1 ) {
$last = array_pop( $decimal );
$second_last = array_pop( $decimal );
$decimal[] = $second_last . $last;
}
$result = $matches['whole'];
if( $decimal[0] != '' ) {
if( count( $decimal ) == 1 ) {
$tmp_decimal = $decimal[0];
} else {
$last_decimals = array_splice( $decimal, 1 );
$tmp_decimal = implode( '', array_map( create_function( '$a', 'return "<span style=\"margin-left:0.25em\">$a</span>";' ) , $last_decimals ) );
$tmp_decimal = $decimal[0] . $tmp_decimal;
}
$result .= ".$tmp_decimal";
}
if( $matches['uncertainty'] ) {
$result .= "($matches[uncertainty])";
}
if( $matches['base'] ) {
$result .= "&nbsp;×&nbsp;$matches[base]<sup>$matches[exponent]</sup>";
}
if( $matches['unit'] ) {
$result .= "&nbsp;$matches[unit]";
}
return $result;
}
?>

File Metadata

Mime Type
text/x-php
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
4330
Default Alt Text
test.php (1 KB)

Event Timeline