How can bad code (such as script injections) be filtered out from BBCode in PHP?

To filter out bad code, such as script injections, from BBCode in PHP, you can use a combination of regular expressions and PHP functions like `strip_tags` and `htmlspecialchars` to sanitize the input. By removing any potentially harmful tags or scripts, you can prevent malicious code from being executed on your website.

// Sample PHP code snippet to filter out bad code from BBCode
$bbcode = "[b]Hello[/b] <script>alert('XSS attack!')</script>";
$filtered_bbcode = strip_tags($bbcode);
echo htmlspecialchars($filtered_bbcode);