How can regular expressions be effectively used in PHP to manipulate and format data like dates?
Regular expressions can be effectively used in PHP to manipulate and format data like dates by matching patterns within the date string and then applying the desired formatting. For example, you can use regular expressions to extract the day, month, and year components of a date and then reformat them in a different order. This can be particularly useful when working with dates in different formats or when you need to standardize the date format across your application.
$date = "2021-10-15";
$pattern = "/(\d{4})-(\d{2})-(\d{2})/";
$replacement = "$3-$2-$1";
$formatted_date = preg_replace($pattern, $replacement, $date);
echo $formatted_date; // Output: 15-10-2021