That's not the right approach, as that deletes the special characters from the tweet text.
I searched online and a better approach would be to simply use the urlenconde php function, which encodes a string to be used in a query part of a URL. Simply do this:
$rawTitle = $input->getString('title', '');
$cleanTitle = urlencode($rawTitle);
I tested this and it works, and it preserves special characters in the tweet text.
Note: I also added
', '' after
$input->getString('title') to suppress a PHP warning about null title.