taxonomy_get_term_by_name() and Encoded Characters
Drupal’s API method
taxonomy\_get\_term\_by\_name() chokes and fails to return the taxonomy term when the term name has encoded characters. The trouble is that term names are usually encoded by the time you pull them from the
$variables array in a hook. This means that a taxonomy term with an ampersand would not get looked up properly, since the ampersand is encoded as
& So doing a lookup of a taxonomy term “Help & Resources” actually ends up feeding “Help
& Resources” to the method.
The solution is to sanitize the term name before the lookup, like so:
$term_name_sanitized = str_replace('&', '&', $variables['title']);
$term_by_name = taxonomy_get_term_by_name($term_name_sanitized, 'article_category');
article_category is the vocabulary name, and
$vars['title'] is the taxonomy name (accessed from the variables array in a hook).