What are some best practices for cleaning up text in PHP, especially when dealing with text from previous scripts that may contain unwanted characters and formatting?

When dealing with text from previous scripts that may contain unwanted characters and formatting, it's important to clean up the text to ensure it is in a consistent and readable format. One way to achieve this is by using PHP functions like `strip_tags()` to remove HTML tags, `trim()` to remove whitespace, and `htmlspecialchars()` to convert special characters to HTML entities.

// Example code snippet for cleaning up text in PHP
$text = "<p>This is some <b>bold</b> text with unwanted <script>alert('malicious script')</script> tags.</p>";
$clean_text = htmlspecialchars(trim(strip_tags($text)));
echo $clean_text;