8.3.x bootstrap.inc drupal_validate_utf8($text)
8.0.x bootstrap.inc drupal_validate_utf8($text)
8.1.x bootstrap.inc drupal_validate_utf8($text)
8.2.x bootstrap.inc drupal_validate_utf8($text)
8.4.x bootstrap.inc drupal_validate_utf8($text)
4.7.x bootstrap.inc drupal_validate_utf8($text)
5.x bootstrap.inc drupal_validate_utf8($text)
6.x bootstrap.inc drupal_validate_utf8($text)
7.x bootstrap.inc drupal_validate_utf8($text)

Checks whether a string is valid UTF-8.

All functions designed to filter input should use drupal_validate_utf8 to ensure they operate on valid UTF-8 strings to prevent bypass of the filter.

When text containing an invalid UTF-8 lead byte (0xC0 - 0xFF) is presented as UTF-8 to Internet Explorer 6, the program may misinterpret subsequent bytes. When these subsequent bytes are HTML control characters such as quotes or angle brackets, parts of the text that were deemed safe by filters end up in locations that are potentially unsafe; An onerror attribute that is outside of a tag, and thus deemed safe by a filter, can be interpreted by the browser as if it were inside the tag.

The function does not return FALSE for strings containing character codes above U+10FFFF, even though these are prohibited by RFC 3629.

Parameters

$text: The text to check.

Return value

TRUE if the text is valid UTF-8, FALSE if not.

1 call to drupal_validate_utf8()
filter_xss in includes/common.inc
Filters HTML to prevent cross-site-scripting (XSS) vulnerabilities.

File

includes/bootstrap.inc, line 1870
Functions that need to be loaded on every Drupal request.

Code

function drupal_validate_utf8($text) {
  if (strlen($text) == 0) {
    return TRUE;
  }
  // With the PCRE_UTF8 modifier 'u', preg_match() fails silently on strings
  // containing invalid UTF-8 byte sequences. It does not reject character
  // codes above U+10FFFF (represented by 4 or more octets), though.
  return (preg_match('/^./us', $text) == 1);
}