How can PHP be used to validate and format user input for decimal numbers with different decimal separators?

When dealing with user input for decimal numbers with different decimal separators, it is important to validate and format the input to ensure consistency. One way to achieve this is by using PHP functions such as str_replace() to replace any non-standard decimal separators with a standard one, such as a period (.) before further processing the input.

$input = "123,45"; // User input with comma as decimal separator
$cleaned_input = str_replace(',', '.', $input); // Replace comma with period
$decimal_number = floatval($cleaned_input); // Convert the cleaned input to a float
echo $decimal_number; // Output: 123.45