I am programatically creating taxonomy terms from a JSON object. I have a simple script that iterates over the converted JSON, and creates a taxonomy hierarchy. Since these terms come from a client-generated CSV they have random spaces and characters in them I want to sanitize/normalize them before I add them using taxonomy_vocabulary_save()

I've looked at the list of drupal Sanitization Functions, but those all seem to deal with HTML/JS strings. Is there a drupal way for me to format these strings, or should I just use a PHP regex to do it?

2 Answers 2


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.

  • 1
    for the love of god try to normalize them to like lowercase numbers and letters and nothing that requires html special characters. that way if you build select lists or rewrite the url to include the term title you get something very simple with no weirdness.
    – tenken
    Mar 8, 2013 at 2:00

You should use PHP regular expressions along with Drupal sanitization functions.

  • 2
    Consider explain your answer regarding the context.
    – niksmac
    Mar 8, 2013 at 1:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.