In PHP, how can a function like str2num be used to handle conversion of numeric strings with different decimal separators?

When dealing with numeric strings with different decimal separators, such as periods or commas, a function like str2num can be used to handle the conversion. This function can take a string as input, detect the decimal separator used, and convert it to a numeric value that PHP can work with. By implementing this function, you can ensure that numeric strings with different decimal separators are correctly converted to numbers in your PHP code.

function str2num($str) {
    $decimal_separator = '.';
    $str = str_replace(',', $decimal_separator, $str);
    return floatval($str);
}

// Example usage
$numeric_str1 = "10.5";
$numeric_str2 = "15,75";

$num1 = str2num($numeric_str1);
$num2 = str2num($numeric_str2);

echo $num1; // Output: 10.5
echo $num2; // Output: 15.75