What are the potential pitfalls of using str_replace in PHP for filtering entries?

Using str_replace for filtering entries in PHP can be problematic because it only replaces exact matches, leaving room for potential bypasses using variations of the input. To solve this issue, it is recommended to use regular expressions with functions like preg_replace to provide more robust filtering capabilities.

// Example of using preg_replace for filtering entries
$input = "Hello, <script>alert('XSS');</script>";
$filtered_input = preg_replace("/<script.*?>|<\/script>/i", "", $input);
echo $filtered_input;