What alternative function can be used instead of str_replace to handle the issue of replacing specific text in PHP?

The issue with using str_replace to replace specific text in PHP is that it only replaces exact matches, which can be limiting if you need to replace text that may vary slightly. An alternative function that can be used is preg_replace, which allows for more flexible pattern matching using regular expressions.

// Using preg_replace to replace specific text in PHP
$text = "The quick brown fox jumps over the lazy dog.";
$new_text = preg_replace("/brown fox/", "red fox", $text);

echo $new_text; // Output: The quick red fox jumps over the lazy dog.