\d?[\d,]*)\.?(?P\d?[\d ]*)\s* (?:\((?P\d+)\))?\s* (?:\*(?P\d+)\^(?P\d+))?\s* (?P\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 "$a";' ) , $last_decimals ) ); $tmp_decimal = $decimal[0] . $tmp_decimal; } $result .= ".$tmp_decimal"; } if( $matches['uncertainty'] ) { $result .= "($matches[uncertainty])"; } if( $matches['base'] ) { $result .= " × $matches[base]$matches[exponent]"; } if( $matches['unit'] ) { $result .= " $matches[unit]"; } return $result; } ?>