What are some common pitfalls or mistakes to avoid when manipulating strings in PHP for file naming purposes?

One common pitfall when manipulating strings for file naming purposes in PHP is forgetting to sanitize the input to ensure that the filename is valid. This can lead to errors or security vulnerabilities if special characters or invalid characters are included in the filename. To avoid this, you can use the `preg_replace()` function to remove any invalid characters from the string before using it as a filename.

$filename = "My File Name: Invalid Characters.txt";
$filename = preg_replace("/[^a-zA-Z0-9.]/", "", $filename);
echo $filename; // Output: MyFileNameInvalidCharacters.txt