Are there any potential pitfalls in using str_replace() to replace consecutive hyphens in a PHP string?
Using str_replace() to replace consecutive hyphens in a PHP string may result in unexpected behavior if the string contains multiple consecutive hyphens. This is because str_replace() will replace each individual hyphen separately, potentially leading to incorrect results. To solve this issue, you can use a regular expression with the preg_replace() function to replace all consecutive hyphens with a single hyphen.
$string = "hello---world";
$fixed_string = preg_replace('/-+/', '-', $string);
echo $fixed_string; // Output: hello-world