What is the difference between str_replace and preg_replace in handling line breaks in PHP?

When dealing with line breaks in PHP, the main difference between str_replace and preg_replace is that str_replace is a simple string replacement function that does not support regular expressions, while preg_replace is a more powerful function that allows for pattern matching using regular expressions. If you need to replace line breaks in a string with another character or string, str_replace can be used. However, if you need more complex pattern matching for line breaks, such as replacing multiple consecutive line breaks with a single one, preg_replace with a regular expression pattern would be more suitable.

// Using str_replace to replace line breaks with a specific character
$string = "Hello\nWorld";
$new_string = str_replace("\n", " ", $string);
echo $new_string;

// Using preg_replace to replace multiple consecutive line breaks with a single one
$string = "Hello\n\n\nWorld";
$new_string = preg_replace("/\n+/", "\n", $string);
echo $new_string;