Chris@0: 'UTF-8', Chris@0: "\xFE\xFF" => 'UTF-16BE', Chris@0: "\xFF\xFE" => 'UTF-16LE', Chris@0: "\x00\x00\xFE\xFF" => 'UTF-32BE', Chris@0: "\xFF\xFE\x00\x00" => 'UTF-32LE', Chris@0: "\x2B\x2F\x76\x38" => 'UTF-7', Chris@0: "\x2B\x2F\x76\x39" => 'UTF-7', Chris@0: "\x2B\x2F\x76\x2B" => 'UTF-7', Chris@0: "\x2B\x2F\x76\x2F" => 'UTF-7', Chris@0: "\x2B\x2F\x76\x38\x2D" => 'UTF-7', Chris@0: ]; Chris@0: Chris@0: foreach ($bomMap as $bom => $encoding) { Chris@0: if (strpos($data, $bom) === 0) { Chris@0: return $encoding; Chris@0: } Chris@0: } Chris@0: return FALSE; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Converts data to UTF-8. Chris@0: * Chris@0: * Requires the iconv, GNU recode or mbstring PHP extension. Chris@0: * Chris@0: * @param string $data Chris@0: * The data to be converted. Chris@0: * @param string $encoding Chris@0: * The encoding that the data is in. Chris@0: * Chris@0: * @return string|bool Chris@0: * Converted data or FALSE. Chris@0: */ Chris@0: public static function convertToUtf8($data, $encoding) { Chris@0: if (function_exists('iconv')) { Chris@0: return @iconv($encoding, 'utf-8', $data); Chris@0: } Chris@0: elseif (function_exists('mb_convert_encoding')) { Chris@0: return @mb_convert_encoding($data, 'utf-8', $encoding); Chris@0: } Chris@0: elseif (function_exists('recode_string')) { Chris@0: return @recode_string($encoding . '..utf-8', $data); Chris@0: } Chris@0: // Cannot convert. Chris@0: return FALSE; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Truncates a UTF-8-encoded string safely to a number of bytes. Chris@0: * Chris@0: * If the end position is in the middle of a UTF-8 sequence, it scans backwards Chris@0: * until the beginning of the byte sequence. Chris@0: * Chris@0: * Use this function whenever you want to chop off a string at an unsure Chris@0: * location. On the other hand, if you're sure that you're splitting on a Chris@0: * character boundary (e.g. after using strpos() or similar), you can safely Chris@0: * use substr() instead. Chris@0: * Chris@0: * @param string $string Chris@0: * The string to truncate. Chris@0: * @param int $len Chris@0: * An upper limit on the returned string length. Chris@0: * Chris@0: * @return string Chris@0: * The truncated string. Chris@0: */ Chris@0: public static function truncateBytes($string, $len) { Chris@0: if (strlen($string) <= $len) { Chris@0: return $string; Chris@0: } Chris@0: if ((ord($string[$len]) < 0x80) || (ord($string[$len]) >= 0xC0)) { Chris@0: return substr($string, 0, $len); Chris@0: } Chris@0: // Scan backwards to beginning of the byte sequence. Chris@0: // @todo Make the code more readable in https://www.drupal.org/node/2911497. Chris@0: while (--$len >= 0 && ord($string[$len]) >= 0x80 && ord($string[$len]) < 0xC0) { Chris@0: } Chris@0: Chris@0: return substr($string, 0, $len); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Counts the number of characters in a UTF-8 string. Chris@0: * Chris@0: * This is less than or equal to the byte count. Chris@0: * Chris@0: * @param string $text Chris@0: * The string to run the operation on. Chris@0: * Chris@0: * @return int Chris@0: * The length of the string. Chris@0: */ Chris@0: public static function strlen($text) { Chris@0: if (static::getStatus() == static::STATUS_MULTIBYTE) { Chris@0: return mb_strlen($text); Chris@0: } Chris@0: else { Chris@0: // Do not count UTF-8 continuation bytes. Chris@0: return strlen(preg_replace("/[\x80-\xBF]/", '', $text)); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Converts a UTF-8 string to uppercase. Chris@0: * Chris@0: * @param string $text Chris@0: * The string to run the operation on. Chris@0: * Chris@0: * @return string Chris@0: * The string in uppercase. Chris@0: */ Chris@0: public static function strtoupper($text) { Chris@0: if (static::getStatus() == static::STATUS_MULTIBYTE) { Chris@0: return mb_strtoupper($text); Chris@0: } Chris@0: else { Chris@0: // Use C-locale for ASCII-only uppercase. Chris@0: $text = strtoupper($text); Chris@0: // Case flip Latin-1 accented letters. Chris@0: $text = preg_replace_callback('/\xC3[\xA0-\xB6\xB8-\xBE]/', '\Drupal\Component\Utility\Unicode::caseFlip', $text); Chris@0: return $text; Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Converts a UTF-8 string to lowercase. Chris@0: * Chris@0: * @param string $text Chris@0: * The string to run the operation on. Chris@0: * Chris@0: * @return string Chris@0: * The string in lowercase. Chris@0: */ Chris@0: public static function strtolower($text) { Chris@0: if (static::getStatus() == static::STATUS_MULTIBYTE) { Chris@0: return mb_strtolower($text); Chris@0: } Chris@0: else { Chris@0: // Use C-locale for ASCII-only lowercase. Chris@0: $text = strtolower($text); Chris@0: // Case flip Latin-1 accented letters. Chris@0: $text = preg_replace_callback('/\xC3[\x80-\x96\x98-\x9E]/', '\Drupal\Component\Utility\Unicode::caseFlip', $text); Chris@0: return $text; Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Capitalizes the first character of a UTF-8 string. Chris@0: * Chris@0: * @param string $text Chris@0: * The string to convert. Chris@0: * Chris@0: * @return string Chris@0: * The string with the first character as uppercase. Chris@0: */ Chris@0: public static function ucfirst($text) { Chris@0: return static::strtoupper(static::substr($text, 0, 1)) . static::substr($text, 1); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Converts the first character of a UTF-8 string to lowercase. Chris@0: * Chris@0: * @param string $text Chris@0: * The string that will be converted. Chris@0: * Chris@0: * @return string Chris@0: * The string with the first character as lowercase. Chris@0: * Chris@0: * @ingroup php_wrappers Chris@0: */ Chris@0: public static function lcfirst($text) { Chris@0: // Note: no mbstring equivalent! Chris@0: return static::strtolower(static::substr($text, 0, 1)) . static::substr($text, 1); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Capitalizes the first character of each word in a UTF-8 string. Chris@0: * Chris@0: * @param string $text Chris@0: * The text that will be converted. Chris@0: * Chris@0: * @return string Chris@0: * The input $text with each word capitalized. Chris@0: * Chris@0: * @ingroup php_wrappers Chris@0: */ Chris@0: public static function ucwords($text) { Chris@0: $regex = '/(^|[' . static::PREG_CLASS_WORD_BOUNDARY . '])([^' . static::PREG_CLASS_WORD_BOUNDARY . '])/u'; Chris@0: return preg_replace_callback($regex, function (array $matches) { Chris@0: return $matches[1] . Unicode::strtoupper($matches[2]); Chris@0: }, $text); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Cuts off a piece of a string based on character indices and counts. Chris@0: * Chris@0: * Follows the same behavior as PHP's own substr() function. Note that for Chris@0: * cutting off a string at a known character/substring location, the usage of Chris@0: * PHP's normal strpos/substr is safe and much faster. Chris@0: * Chris@0: * @param string $text Chris@0: * The input string. Chris@0: * @param int $start Chris@0: * The position at which to start reading. Chris@0: * @param int $length Chris@0: * The number of characters to read. Chris@0: * Chris@0: * @return string Chris@0: * The shortened string. Chris@0: */ Chris@0: public static function substr($text, $start, $length = NULL) { Chris@0: if (static::getStatus() == static::STATUS_MULTIBYTE) { Chris@0: return $length === NULL ? mb_substr($text, $start) : mb_substr($text, $start, $length); Chris@0: } Chris@0: else { Chris@0: $strlen = strlen($text); Chris@0: // Find the starting byte offset. Chris@0: $bytes = 0; Chris@0: if ($start > 0) { Chris@0: // Count all the characters except continuation bytes from the start Chris@0: // until we have found $start characters or the end of the string. Chris@0: $bytes = -1; $chars = -1; Chris@0: while ($bytes < $strlen - 1 && $chars < $start) { Chris@0: $bytes++; Chris@0: $c = ord($text[$bytes]); Chris@0: if ($c < 0x80 || $c >= 0xC0) { Chris@0: $chars++; Chris@0: } Chris@0: } Chris@0: } Chris@0: elseif ($start < 0) { Chris@0: // Count all the characters except continuation bytes from the end Chris@0: // until we have found abs($start) characters. Chris@0: $start = abs($start); Chris@0: $bytes = $strlen; $chars = 0; Chris@0: while ($bytes > 0 && $chars < $start) { Chris@0: $bytes--; Chris@0: $c = ord($text[$bytes]); Chris@0: if ($c < 0x80 || $c >= 0xC0) { Chris@0: $chars++; Chris@0: } Chris@0: } Chris@0: } Chris@0: $istart = $bytes; Chris@0: Chris@0: // Find the ending byte offset. Chris@0: if ($length === NULL) { Chris@0: $iend = $strlen; Chris@0: } Chris@0: elseif ($length > 0) { Chris@0: // Count all the characters except continuation bytes from the starting Chris@0: // index until we have found $length characters or reached the end of Chris@0: // the string, then backtrace one byte. Chris@0: $iend = $istart - 1; Chris@0: $chars = -1; Chris@0: $last_real = FALSE; Chris@0: while ($iend < $strlen - 1 && $chars < $length) { Chris@0: $iend++; Chris@0: $c = ord($text[$iend]); Chris@0: $last_real = FALSE; Chris@0: if ($c < 0x80 || $c >= 0xC0) { Chris@0: $chars++; Chris@0: $last_real = TRUE; Chris@0: } Chris@0: } Chris@0: // Backtrace one byte if the last character we found was a real Chris@0: // character and we don't need it. Chris@0: if ($last_real && $chars >= $length) { Chris@0: $iend--; Chris@0: } Chris@0: } Chris@0: elseif ($length < 0) { Chris@0: // Count all the characters except continuation bytes from the end Chris@0: // until we have found abs($start) characters, then backtrace one byte. Chris@0: $length = abs($length); Chris@0: $iend = $strlen; $chars = 0; Chris@0: while ($iend > 0 && $chars < $length) { Chris@0: $iend--; Chris@0: $c = ord($text[$iend]); Chris@0: if ($c < 0x80 || $c >= 0xC0) { Chris@0: $chars++; Chris@0: } Chris@0: } Chris@0: // Backtrace one byte if we are not at the beginning of the string. Chris@0: if ($iend > 0) { Chris@0: $iend--; Chris@0: } Chris@0: } Chris@0: else { Chris@0: // $length == 0, return an empty string. Chris@0: return ''; Chris@0: } Chris@0: Chris@0: return substr($text, $istart, max(0, $iend - $istart + 1)); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Truncates a UTF-8-encoded string safely to a number of characters. Chris@0: * Chris@0: * @param string $string Chris@0: * The string to truncate. Chris@0: * @param int $max_length Chris@0: * An upper limit on the returned string length, including trailing ellipsis Chris@0: * if $add_ellipsis is TRUE. Chris@0: * @param bool $wordsafe Chris@0: * If TRUE, attempt to truncate on a word boundary. Word boundaries are Chris@0: * spaces, punctuation, and Unicode characters used as word boundaries in Chris@0: * non-Latin languages; see Unicode::PREG_CLASS_WORD_BOUNDARY for more Chris@0: * information. If a word boundary cannot be found that would make the length Chris@0: * of the returned string fall within length guidelines (see parameters Chris@0: * $max_length and $min_wordsafe_length), word boundaries are ignored. Chris@0: * @param bool $add_ellipsis Chris@0: * If TRUE, add '...' to the end of the truncated string (defaults to Chris@0: * FALSE). The string length will still fall within $max_length. Chris@0: * @param int $min_wordsafe_length Chris@0: * If $wordsafe is TRUE, the minimum acceptable length for truncation (before Chris@0: * adding an ellipsis, if $add_ellipsis is TRUE). Has no effect if $wordsafe Chris@0: * is FALSE. This can be used to prevent having a very short resulting string Chris@0: * that will not be understandable. For instance, if you are truncating the Chris@0: * string "See myverylongurlexample.com for more information" to a word-safe Chris@0: * return length of 20, the only available word boundary within 20 characters Chris@0: * is after the word "See", which wouldn't leave a very informative string. If Chris@0: * you had set $min_wordsafe_length to 10, though, the function would realise Chris@0: * that "See" alone is too short, and would then just truncate ignoring word Chris@0: * boundaries, giving you "See myverylongurl..." (assuming you had set Chris@0: * $add_ellipses to TRUE). Chris@0: * Chris@0: * @return string Chris@0: * The truncated string. Chris@0: */ Chris@0: public static function truncate($string, $max_length, $wordsafe = FALSE, $add_ellipsis = FALSE, $min_wordsafe_length = 1) { Chris@0: $ellipsis = ''; Chris@0: $max_length = max($max_length, 0); Chris@0: $min_wordsafe_length = max($min_wordsafe_length, 0); Chris@0: Chris@0: if (static::strlen($string) <= $max_length) { Chris@0: // No truncation needed, so don't add ellipsis, just return. Chris@0: return $string; Chris@0: } Chris@0: Chris@0: if ($add_ellipsis) { Chris@0: // Truncate ellipsis in case $max_length is small. Chris@0: $ellipsis = static::substr('…', 0, $max_length); Chris@0: $max_length -= static::strlen($ellipsis); Chris@0: $max_length = max($max_length, 0); Chris@0: } Chris@0: Chris@0: if ($max_length <= $min_wordsafe_length) { Chris@0: // Do not attempt word-safe if lengths are bad. Chris@0: $wordsafe = FALSE; Chris@0: } Chris@0: Chris@0: if ($wordsafe) { Chris@0: $matches = []; Chris@0: // Find the last word boundary, if there is one within $min_wordsafe_length Chris@0: // to $max_length characters. preg_match() is always greedy, so it will Chris@0: // find the longest string possible. Chris@0: $found = preg_match('/^(.{' . $min_wordsafe_length . ',' . $max_length . '})[' . Unicode::PREG_CLASS_WORD_BOUNDARY . ']/u', $string, $matches); Chris@0: if ($found) { Chris@0: $string = $matches[1]; Chris@0: } Chris@0: else { Chris@0: $string = static::substr($string, 0, $max_length); Chris@0: } Chris@0: } Chris@0: else { Chris@0: $string = static::substr($string, 0, $max_length); Chris@0: } Chris@0: Chris@0: if ($add_ellipsis) { Chris@0: // If we're adding an ellipsis, remove any trailing periods. Chris@0: $string = rtrim($string, '.'); Chris@0: Chris@0: $string .= $ellipsis; Chris@0: } Chris@0: Chris@0: return $string; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Compares UTF-8-encoded strings in a binary safe case-insensitive manner. Chris@0: * Chris@0: * @param string $str1 Chris@0: * The first string. Chris@0: * @param string $str2 Chris@0: * The second string. Chris@0: * Chris@0: * @return int Chris@0: * Returns < 0 if $str1 is less than $str2; > 0 if $str1 is greater than Chris@0: * $str2, and 0 if they are equal. Chris@0: */ Chris@0: public static function strcasecmp($str1, $str2) { Chris@0: return strcmp(static::strtoupper($str1), static::strtoupper($str2)); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Encodes MIME/HTTP headers that contain incorrectly encoded characters. Chris@0: * Chris@0: * For example, Unicode::mimeHeaderEncode('tést.txt') returns Chris@0: * "=?UTF-8?B?dMOpc3QudHh0?=". Chris@0: * Chris@0: * See http://www.rfc-editor.org/rfc/rfc2047.txt for more information. Chris@0: * Chris@0: * Notes: Chris@0: * - Only encode strings that contain non-ASCII characters. Chris@0: * - We progressively cut-off a chunk with self::truncateBytes(). This ensures Chris@0: * each chunk starts and ends on a character boundary. Chris@0: * - Using \n as the chunk separator may cause problems on some systems and Chris@0: * may have to be changed to \r\n or \r. Chris@0: * Chris@0: * @param string $string Chris@0: * The header to encode. Chris@12: * @param bool $shorten Chris@12: * If TRUE, only return the first chunk of a multi-chunk encoded string. Chris@0: * Chris@0: * @return string Chris@0: * The mime-encoded header. Chris@0: */ Chris@12: public static function mimeHeaderEncode($string, $shorten = FALSE) { Chris@0: if (preg_match('/[^\x20-\x7E]/', $string)) { Chris@0: // floor((75 - strlen("=?UTF-8?B??=")) * 0.75); Chris@0: $chunk_size = 47; Chris@0: $len = strlen($string); Chris@0: $output = ''; Chris@0: while ($len > 0) { Chris@0: $chunk = static::truncateBytes($string, $chunk_size); Chris@0: $output .= ' =?UTF-8?B?' . base64_encode($chunk) . "?=\n"; Chris@12: if ($shorten) { Chris@12: break; Chris@12: } Chris@0: $c = strlen($chunk); Chris@0: $string = substr($string, $c); Chris@0: $len -= $c; Chris@0: } Chris@0: return trim($output); Chris@0: } Chris@0: return $string; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Decodes MIME/HTTP encoded header values. Chris@0: * Chris@0: * @param string $header Chris@0: * The header to decode. Chris@0: * Chris@0: * @return string Chris@0: * The mime-decoded header. Chris@0: */ Chris@0: public static function mimeHeaderDecode($header) { Chris@0: $callback = function ($matches) { Chris@0: $data = ($matches[2] == 'B') ? base64_decode($matches[3]) : str_replace('_', ' ', quoted_printable_decode($matches[3])); Chris@0: if (strtolower($matches[1]) != 'utf-8') { Chris@0: $data = static::convertToUtf8($data, $matches[1]); Chris@0: } Chris@0: return $data; Chris@0: }; Chris@0: // First step: encoded chunks followed by other encoded chunks (need to collapse whitespace) Chris@0: $header = preg_replace_callback('/=\?([^?]+)\?(Q|B)\?([^?]+|\?(?!=))\?=\s+(?==\?)/', $callback, $header); Chris@0: // Second step: remaining chunks (do not collapse whitespace) Chris@0: return preg_replace_callback('/=\?([^?]+)\?(Q|B)\?([^?]+|\?(?!=))\?=/', $callback, $header); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Flip U+C0-U+DE to U+E0-U+FD and back. Can be used as preg_replace callback. Chris@0: * Chris@0: * @param array $matches Chris@0: * An array of matches by preg_replace_callback(). Chris@0: * Chris@0: * @return string Chris@0: * The flipped text. Chris@0: */ Chris@0: public static function caseFlip($matches) { Chris@0: return $matches[0][0] . chr(ord($matches[0][1]) ^ 32); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks whether a string is valid UTF-8. Chris@0: * Chris@0: * All functions designed to filter input should use drupal_validate_utf8 Chris@0: * to ensure they operate on valid UTF-8 strings to prevent bypass of the Chris@0: * filter. Chris@0: * Chris@0: * When text containing an invalid UTF-8 lead byte (0xC0 - 0xFF) is presented Chris@0: * as UTF-8 to Internet Explorer 6, the program may misinterpret subsequent Chris@0: * bytes. When these subsequent bytes are HTML control characters such as Chris@0: * quotes or angle brackets, parts of the text that were deemed safe by filters Chris@0: * end up in locations that are potentially unsafe; An onerror attribute that Chris@0: * is outside of a tag, and thus deemed safe by a filter, can be interpreted Chris@0: * by the browser as if it were inside the tag. Chris@0: * Chris@0: * The function does not return FALSE for strings containing character codes Chris@0: * above U+10FFFF, even though these are prohibited by RFC 3629. Chris@0: * Chris@0: * @param string $text Chris@0: * The text to check. Chris@0: * Chris@0: * @return bool Chris@0: * TRUE if the text is valid UTF-8, FALSE if not. Chris@0: */ Chris@0: public static function validateUtf8($text) { Chris@0: if (strlen($text) == 0) { Chris@0: return TRUE; Chris@0: } Chris@0: // With the PCRE_UTF8 modifier 'u', preg_match() fails silently on strings Chris@0: // containing invalid UTF-8 byte sequences. It does not reject character Chris@0: // codes above U+10FFFF (represented by 4 or more octets), though. Chris@0: return (preg_match('/^./us', $text) == 1); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Finds the position of the first occurrence of a string in another string. Chris@0: * Chris@0: * @param string $haystack Chris@0: * The string to search in. Chris@0: * @param string $needle Chris@0: * The string to find in $haystack. Chris@0: * @param int $offset Chris@0: * If specified, start the search at this number of characters from the Chris@0: * beginning (default 0). Chris@0: * Chris@0: * @return int|false Chris@0: * The position where $needle occurs in $haystack, always relative to the Chris@0: * beginning (independent of $offset), or FALSE if not found. Note that Chris@0: * a return value of 0 is not the same as FALSE. Chris@0: */ Chris@0: public static function strpos($haystack, $needle, $offset = 0) { Chris@0: if (static::getStatus() == static::STATUS_MULTIBYTE) { Chris@0: return mb_strpos($haystack, $needle, $offset); Chris@0: } Chris@0: else { Chris@0: // Remove Unicode continuation characters, to be compatible with Chris@0: // Unicode::strlen() and Unicode::substr(). Chris@0: $haystack = preg_replace("/[\x80-\xBF]/", '', $haystack); Chris@0: $needle = preg_replace("/[\x80-\xBF]/", '', $needle); Chris@0: return strpos($haystack, $needle, $offset); Chris@0: } Chris@0: } Chris@0: Chris@0: }