What are the potential pitfalls of using preg_replace or str_replace in PHP for formatting numbers?
Using preg_replace or str_replace for formatting numbers can lead to unexpected results, especially with decimal points and thousands separators. It is better to use number_format() function in PHP, which is specifically designed for formatting numbers with precision and proper handling of decimal points and separators.
// Example of using number_format() for formatting numbers
$number = 1234567.89;
$formatted_number = number_format($number, 2, '.', ',');
echo $formatted_number; // Output: 1,234,567.89