Sanitizing strings before adding as taxonomy term
Running the text through check_plain() will sanitize anything dangerous, giving you the same result you'd get if you were to input the same title as part of the normal 'create term' process via Drupal's UI. I'd also run it through trim() to get rid of preceding/trailing whitespace. Beyond that, yes, you might need to do some custom regex to clean up things in your data, but that's impossible to say without seeing your data.