How can one effectively handle numeric values with non-standard formatting, such as using number_format in PHP?

When dealing with numeric values with non-standard formatting in PHP, such as having commas or decimals in unexpected places, you can use the number_format function to properly format the number. This function allows you to specify the number of decimal places, thousands separator, and decimal point to ensure consistent formatting of numeric values.

$number = "1,234.56"; // Numeric value with non-standard formatting
$cleaned_number = str_replace(',', '', $number); // Remove commas
$cleaned_number = floatval($cleaned_number); // Convert to float
$formatted_number = number_format($cleaned_number, 2, '.', ','); // Format the number with 2 decimal places, using period as decimal point and comma as thousands separator

echo $formatted_number; // Output: 1,234.56