What are some potential pitfalls of manually parsing and formatting text with bbcode in PHP?

One potential pitfall of manually parsing and formatting text with bbcode in PHP is the risk of introducing security vulnerabilities if input is not properly sanitized. To solve this issue, it is important to use functions like `htmlspecialchars()` to escape special characters in user input before applying bbcode formatting.

// Example of properly sanitizing user input before applying bbcode formatting
$input = "<script>alert('XSS attack!')</script>";
$sanitized_input = htmlspecialchars($input);

// Apply bbcode formatting to the sanitized input
$formatted_text = bbcode_format($sanitized_input);

echo $formatted_text;