Community Documentation

Use Drupal Unicode functions for strings

Last updated November 8, 2011. Created by jhodgdon on May 26, 2009.
Edited by kiamlaluno, sun. Log in to edit this page.

If you are writing a module or theme, consider that it may be used on web sites around the world, some of which use languages whose characters are multi-byte Unicode, rather than single-byte ASCII or European formats. Some of the built-in PHP string processing functions do not work correctly on multi-byte text.

For that reason, Drupal provides replacements for PHP's built-in text functions. You should use these replacements when you are programming for Drupal, except where noted. The Coder module can be used to check your module for these replacement functions.

Here are the replacement functions:

For a complete list of all functions wrapping a regular PHP function in Drupal: PHP wrapper functions

If you are programming with text, you should also read the guide to handling text in a secure fashion.

Comments

Two problems

  1. I ran the coder module on my code, on the most sensitive setting, and it didn't flag my use of substr() instead of drupal_substr().
  2. The documentation for drupal_substr() confusingly seems to directly contradict the recommendation here. It says that "for cutting off a string at a known character/substring location [and what else does one use substr() for?], the usage of PHP's normal strpos/substr is safe and much faster." So which one are we supposed to use?