annotate includes/unicode.inc @ 20:e3d20ebd63d1 tip

Added tag 6.9 for changeset 3edae6ecd6c6
author Franck Deroche <franck@defr.org>
date Thu, 15 Jan 2009 10:16:10 +0100
parents c1f4ac30525a
children
rev   line source
webmaster@1 1 <?php
webmaster@1 2 // $Id: unicode.inc,v 1.29 2007/12/28 12:02:50 dries Exp $
webmaster@1 3
webmaster@1 4 /**
webmaster@1 5 * Indicates an error during check for PHP unicode support.
webmaster@1 6 */
webmaster@1 7 define('UNICODE_ERROR', -1);
webmaster@1 8
webmaster@1 9 /**
webmaster@1 10 * Indicates that standard PHP (emulated) unicode support is being used.
webmaster@1 11 */
webmaster@1 12 define('UNICODE_SINGLEBYTE', 0);
webmaster@1 13
webmaster@1 14 /**
webmaster@1 15 * Indicates that full unicode support with the PHP mbstring extension is being
webmaster@1 16 * used.
webmaster@1 17 */
webmaster@1 18 define('UNICODE_MULTIBYTE', 1);
webmaster@1 19
webmaster@1 20 /**
webmaster@1 21 * Wrapper around _unicode_check().
webmaster@1 22 */
webmaster@1 23 function unicode_check() {
webmaster@1 24 list($GLOBALS['multibyte']) = _unicode_check();
webmaster@1 25 }
webmaster@1 26
webmaster@1 27 /**
webmaster@1 28 * Perform checks about Unicode support in PHP, and set the right settings if
webmaster@1 29 * needed.
webmaster@1 30 *
webmaster@1 31 * Because Drupal needs to be able to handle text in various encodings, we do
webmaster@1 32 * not support mbstring function overloading. HTTP input/output conversion must
webmaster@1 33 * be disabled for similar reasons.
webmaster@1 34 *
webmaster@1 35 * @param $errors
webmaster@1 36 * Whether to report any fatal errors with form_set_error().
webmaster@1 37 */
webmaster@1 38 function _unicode_check() {
webmaster@1 39 // Ensure translations don't break at install time
webmaster@1 40 $t = get_t();
webmaster@1 41
webmaster@1 42 // Set the standard C locale to ensure consistent, ASCII-only string handling.
webmaster@1 43 setlocale(LC_CTYPE, 'C');
webmaster@1 44
webmaster@1 45 // Check for outdated PCRE library
webmaster@1 46 // Note: we check if U+E2 is in the range U+E0 - U+E1. This test returns TRUE on old PCRE versions.
webmaster@1 47 if (preg_match('/[à-á]/u', 'â')) {
webmaster@1 48 return array(UNICODE_ERROR, $t('The PCRE library in your PHP installation is outdated. This will cause problems when handling Unicode text. If you are running PHP 4.3.3 or higher, make sure you are using the PCRE library supplied by PHP. Please refer to the <a href="@url">PHP PCRE documentation</a> for more information.', array('@url' => 'http://www.php.net/pcre')));
webmaster@1 49 }
webmaster@1 50
webmaster@1 51 // Check for mbstring extension
webmaster@1 52 if (!function_exists('mb_strlen')) {
webmaster@1 53 return array(UNICODE_SINGLEBYTE, $t('Operations on Unicode strings are emulated on a best-effort basis. Install the <a href="@url">PHP mbstring extension</a> for improved Unicode support.', array('@url' => 'http://www.php.net/mbstring')));
webmaster@1 54 }
webmaster@1 55
webmaster@1 56 // Check mbstring configuration
webmaster@1 57 if (ini_get('mbstring.func_overload') != 0) {
webmaster@1 58 return array(UNICODE_ERROR, $t('Multibyte string function overloading in PHP is active and must be disabled. Check the php.ini <em>mbstring.func_overload</em> setting. Please refer to the <a href="@url">PHP mbstring documentation</a> for more information.', array('@url' => 'http://www.php.net/mbstring')));
webmaster@1 59 }
webmaster@1 60 if (ini_get('mbstring.encoding_translation') != 0) {
webmaster@1 61 return array(UNICODE_ERROR, $t('Multibyte string input conversion in PHP is active and must be disabled. Check the php.ini <em>mbstring.encoding_translation</em> setting. Please refer to the <a href="@url">PHP mbstring documentation</a> for more information.', array('@url' => 'http://www.php.net/mbstring')));
webmaster@1 62 }
webmaster@1 63 if (ini_get('mbstring.http_input') != 'pass') {
webmaster@1 64 return array(UNICODE_ERROR, $t('Multibyte string input conversion in PHP is active and must be disabled. Check the php.ini <em>mbstring.http_input</em> setting. Please refer to the <a href="@url">PHP mbstring documentation</a> for more information.', array('@url' => 'http://www.php.net/mbstring')));
webmaster@1 65 }
webmaster@1 66 if (ini_get('mbstring.http_output') != 'pass') {
webmaster@1 67 return array(UNICODE_ERROR, $t('Multibyte string output conversion in PHP is active and must be disabled. Check the php.ini <em>mbstring.http_output</em> setting. Please refer to the <a href="@url">PHP mbstring documentation</a> for more information.', array('@url' => 'http://www.php.net/mbstring')));
webmaster@1 68 }
webmaster@1 69
webmaster@1 70 // Set appropriate configuration
webmaster@1 71 mb_internal_encoding('utf-8');
webmaster@1 72 mb_language('uni');
webmaster@1 73 return array(UNICODE_MULTIBYTE, '');
webmaster@1 74 }
webmaster@1 75
webmaster@1 76 /**
webmaster@1 77 * Return Unicode library status and errors.
webmaster@1 78 */
webmaster@1 79 function unicode_requirements() {
webmaster@1 80 // Ensure translations don't break at install time
webmaster@1 81 $t = get_t();
webmaster@1 82
webmaster@1 83 $libraries = array(
webmaster@1 84 UNICODE_SINGLEBYTE => $t('Standard PHP'),
webmaster@1 85 UNICODE_MULTIBYTE => $t('PHP Mbstring Extension'),
webmaster@1 86 UNICODE_ERROR => $t('Error'),
webmaster@1 87 );
webmaster@1 88 $severities = array(
webmaster@1 89 UNICODE_SINGLEBYTE => REQUIREMENT_WARNING,
webmaster@1 90 UNICODE_MULTIBYTE => REQUIREMENT_OK,
webmaster@1 91 UNICODE_ERROR => REQUIREMENT_ERROR,
webmaster@1 92 );
webmaster@1 93 list($library, $description) = _unicode_check();
webmaster@1 94
webmaster@1 95 $requirements['unicode'] = array(
webmaster@1 96 'title' => $t('Unicode library'),
webmaster@1 97 'value' => $libraries[$library],
webmaster@1 98 );
webmaster@1 99 if ($description) {
webmaster@1 100 $requirements['unicode']['description'] = $description;
webmaster@1 101 }
webmaster@1 102
webmaster@1 103 $requirements['unicode']['severity'] = $severities[$library];
webmaster@1 104
webmaster@1 105 return $requirements;
webmaster@1 106 }
webmaster@1 107
webmaster@1 108 /**
webmaster@1 109 * Prepare a new XML parser.
webmaster@1 110 *
webmaster@1 111 * This is a wrapper around xml_parser_create() which extracts the encoding from
webmaster@1 112 * the XML data first and sets the output encoding to UTF-8. This function should
webmaster@1 113 * be used instead of xml_parser_create(), because PHP 4's XML parser doesn't
webmaster@1 114 * check the input encoding itself. "Starting from PHP 5, the input encoding is
webmaster@1 115 * automatically detected, so that the encoding parameter specifies only the
webmaster@1 116 * output encoding."
webmaster@1 117 *
webmaster@1 118 * This is also where unsupported encodings will be converted. Callers should
webmaster@1 119 * take this into account: $data might have been changed after the call.
webmaster@1 120 *
webmaster@1 121 * @param &$data
webmaster@1 122 * The XML data which will be parsed later.
webmaster@1 123 * @return
webmaster@1 124 * An XML parser object.
webmaster@1 125 */
webmaster@1 126 function drupal_xml_parser_create(&$data) {
webmaster@1 127 // Default XML encoding is UTF-8
webmaster@1 128 $encoding = 'utf-8';
webmaster@1 129 $bom = FALSE;
webmaster@1 130
webmaster@1 131 // Check for UTF-8 byte order mark (PHP5's XML parser doesn't handle it).
webmaster@1 132 if (!strncmp($data, "\xEF\xBB\xBF", 3)) {
webmaster@1 133 $bom = TRUE;
webmaster@1 134 $data = substr($data, 3);
webmaster@1 135 }
webmaster@1 136
webmaster@1 137 // Check for an encoding declaration in the XML prolog if no BOM was found.
webmaster@1 138 if (!$bom && ereg('^<\?xml[^>]+encoding="([^"]+)"', $data, $match)) {
webmaster@1 139 $encoding = $match[1];
webmaster@1 140 }
webmaster@1 141
webmaster@1 142 // Unsupported encodings are converted here into UTF-8.
webmaster@1 143 $php_supported = array('utf-8', 'iso-8859-1', 'us-ascii');
webmaster@1 144 if (!in_array(strtolower($encoding), $php_supported)) {
webmaster@1 145 $out = drupal_convert_to_utf8($data, $encoding);
webmaster@1 146 if ($out !== FALSE) {
webmaster@1 147 $encoding = 'utf-8';
webmaster@1 148 $data = ereg_replace('^(<\?xml[^>]+encoding)="([^"]+)"', '\\1="utf-8"', $out);
webmaster@1 149 }
webmaster@1 150 else {
webmaster@1 151 watchdog('php', 'Could not convert XML encoding %s to UTF-8.', array('%s' => $encoding), WATCHDOG_WARNING);
webmaster@1 152 return 0;
webmaster@1 153 }
webmaster@1 154 }
webmaster@1 155
webmaster@1 156 $xml_parser = xml_parser_create($encoding);
webmaster@1 157 xml_parser_set_option($xml_parser, XML_OPTION_TARGET_ENCODING, 'utf-8');
webmaster@1 158 return $xml_parser;
webmaster@1 159 }
webmaster@1 160
webmaster@1 161 /**
webmaster@1 162 * Convert data to UTF-8
webmaster@1 163 *
webmaster@1 164 * Requires the iconv, GNU recode or mbstring PHP extension.
webmaster@1 165 *
webmaster@1 166 * @param $data
webmaster@1 167 * The data to be converted.
webmaster@1 168 * @param $encoding
webmaster@1 169 * The encoding that the data is in
webmaster@1 170 * @return
webmaster@1 171 * Converted data or FALSE.
webmaster@1 172 */
webmaster@1 173 function drupal_convert_to_utf8($data, $encoding) {
webmaster@1 174 if (function_exists('iconv')) {
webmaster@1 175 $out = @iconv($encoding, 'utf-8', $data);
webmaster@1 176 }
webmaster@1 177 else if (function_exists('mb_convert_encoding')) {
webmaster@1 178 $out = @mb_convert_encoding($data, 'utf-8', $encoding);
webmaster@1 179 }
webmaster@1 180 else if (function_exists('recode_string')) {
webmaster@1 181 $out = @recode_string($encoding .'..utf-8', $data);
webmaster@1 182 }
webmaster@1 183 else {
webmaster@1 184 watchdog('php', 'Unsupported encoding %s. Please install iconv, GNU recode or mbstring for PHP.', array('%s' => $encoding), WATCHDOG_ERROR);
webmaster@1 185 return FALSE;
webmaster@1 186 }
webmaster@1 187
webmaster@1 188 return $out;
webmaster@1 189 }
webmaster@1 190
webmaster@1 191 /**
webmaster@1 192 * Truncate a UTF-8-encoded string safely to a number of bytes.
webmaster@1 193 *
webmaster@1 194 * If the end position is in the middle of a UTF-8 sequence, it scans backwards
webmaster@1 195 * until the beginning of the byte sequence.
webmaster@1 196 *
webmaster@1 197 * Use this function whenever you want to chop off a string at an unsure
webmaster@1 198 * location. On the other hand, if you're sure that you're splitting on a
webmaster@1 199 * character boundary (e.g. after using strpos() or similar), you can safely use
webmaster@1 200 * substr() instead.
webmaster@1 201 *
webmaster@1 202 * @param $string
webmaster@1 203 * The string to truncate.
webmaster@1 204 * @param $len
webmaster@1 205 * An upper limit on the returned string length.
webmaster@1 206 * @return
webmaster@1 207 * The truncated string.
webmaster@1 208 */
webmaster@1 209 function drupal_truncate_bytes($string, $len) {
webmaster@1 210 if (strlen($string) <= $len) {
webmaster@1 211 return $string;
webmaster@1 212 }
webmaster@1 213 if ((ord($string[$len]) < 0x80) || (ord($string[$len]) >= 0xC0)) {
webmaster@1 214 return substr($string, 0, $len);
webmaster@1 215 }
webmaster@1 216 while (--$len >= 0 && ord($string[$len]) >= 0x80 && ord($string[$len]) < 0xC0) {};
webmaster@1 217 return substr($string, 0, $len);
webmaster@1 218 }
webmaster@1 219
webmaster@1 220 /**
webmaster@1 221 * Truncate a UTF-8-encoded string safely to a number of characters.
webmaster@1 222 *
webmaster@1 223 * @param $string
webmaster@1 224 * The string to truncate.
webmaster@1 225 * @param $len
webmaster@1 226 * An upper limit on the returned string length.
webmaster@1 227 * @param $wordsafe
webmaster@1 228 * Flag to truncate at last space within the upper limit. Defaults to FALSE.
webmaster@1 229 * @param $dots
webmaster@1 230 * Flag to add trailing dots. Defaults to FALSE.
webmaster@1 231 * @return
webmaster@1 232 * The truncated string.
webmaster@1 233 */
webmaster@1 234 function truncate_utf8($string, $len, $wordsafe = FALSE, $dots = FALSE) {
webmaster@1 235
webmaster@1 236 if (drupal_strlen($string) <= $len) {
webmaster@1 237 return $string;
webmaster@1 238 }
webmaster@1 239
webmaster@1 240 if ($dots) {
webmaster@1 241 $len -= 4;
webmaster@1 242 }
webmaster@1 243
webmaster@1 244 if ($wordsafe) {
webmaster@1 245 $string = drupal_substr($string, 0, $len + 1); // leave one more character
webmaster@1 246 if ($last_space = strrpos($string, ' ')) { // space exists AND is not on position 0
webmaster@1 247 $string = substr($string, 0, $last_space);
webmaster@1 248 }
webmaster@1 249 else {
webmaster@1 250 $string = drupal_substr($string, 0, $len);
webmaster@1 251 }
webmaster@1 252 }
webmaster@1 253 else {
webmaster@1 254 $string = drupal_substr($string, 0, $len);
webmaster@1 255 }
webmaster@1 256
webmaster@1 257 if ($dots) {
webmaster@1 258 $string .= ' ...';
webmaster@1 259 }
webmaster@1 260
webmaster@1 261 return $string;
webmaster@1 262 }
webmaster@1 263
webmaster@1 264 /**
webmaster@1 265 * Encodes MIME/HTTP header values that contain non-ASCII, UTF-8 encoded
webmaster@1 266 * characters.
webmaster@1 267 *
webmaster@1 268 * For example, mime_header_encode('tést.txt') returns "=?UTF-8?B?dMOpc3QudHh0?=".
webmaster@1 269 *
webmaster@1 270 * See http://www.rfc-editor.org/rfc/rfc2047.txt for more information.
webmaster@1 271 *
webmaster@1 272 * Notes:
webmaster@1 273 * - Only encode strings that contain non-ASCII characters.
webmaster@1 274 * - We progressively cut-off a chunk with truncate_utf8(). This is to ensure
webmaster@1 275 * each chunk starts and ends on a character boundary.
webmaster@1 276 * - Using \n as the chunk separator may cause problems on some systems and may
webmaster@1 277 * have to be changed to \r\n or \r.
webmaster@1 278 */
webmaster@1 279 function mime_header_encode($string) {
webmaster@1 280 if (preg_match('/[^\x20-\x7E]/', $string)) {
webmaster@1 281 $chunk_size = 47; // floor((75 - strlen("=?UTF-8?B??=")) * 0.75);
webmaster@1 282 $len = strlen($string);
webmaster@1 283 $output = '';
webmaster@1 284 while ($len > 0) {
webmaster@1 285 $chunk = drupal_truncate_bytes($string, $chunk_size);
webmaster@1 286 $output .= ' =?UTF-8?B?'. base64_encode($chunk) ."?=\n";
webmaster@1 287 $c = strlen($chunk);
webmaster@1 288 $string = substr($string, $c);
webmaster@1 289 $len -= $c;
webmaster@1 290 }
webmaster@1 291 return trim($output);
webmaster@1 292 }
webmaster@1 293 return $string;
webmaster@1 294 }
webmaster@1 295
webmaster@1 296 /**
webmaster@1 297 * Complement to mime_header_encode
webmaster@1 298 */
webmaster@1 299 function mime_header_decode($header) {
webmaster@1 300 // First step: encoded chunks followed by other encoded chunks (need to collapse whitespace)
webmaster@1 301 $header = preg_replace_callback('/=\?([^?]+)\?(Q|B)\?([^?]+|\?(?!=))\?=\s+(?==\?)/', '_mime_header_decode', $header);
webmaster@1 302 // Second step: remaining chunks (do not collapse whitespace)
webmaster@1 303 return preg_replace_callback('/=\?([^?]+)\?(Q|B)\?([^?]+|\?(?!=))\?=/', '_mime_header_decode', $header);
webmaster@1 304 }
webmaster@1 305
webmaster@1 306 /**
webmaster@1 307 * Helper function to mime_header_decode
webmaster@1 308 */
webmaster@1 309 function _mime_header_decode($matches) {
webmaster@1 310 // Regexp groups:
webmaster@1 311 // 1: Character set name
webmaster@1 312 // 2: Escaping method (Q or B)
webmaster@1 313 // 3: Encoded data
webmaster@1 314 $data = ($matches[2] == 'B') ? base64_decode($matches[3]) : str_replace('_', ' ', quoted_printable_decode($matches[3]));
webmaster@1 315 if (strtolower($matches[1]) != 'utf-8') {
webmaster@1 316 $data = drupal_convert_to_utf8($data, $matches[1]);
webmaster@1 317 }
webmaster@1 318 return $data;
webmaster@1 319 }
webmaster@1 320
webmaster@1 321 /**
webmaster@1 322 * Decode all HTML entities (including numerical ones) to regular UTF-8 bytes.
webmaster@1 323 * Double-escaped entities will only be decoded once ("&amp;lt;" becomes "&lt;", not "<").
webmaster@1 324 *
webmaster@1 325 * @param $text
webmaster@1 326 * The text to decode entities in.
webmaster@1 327 * @param $exclude
webmaster@1 328 * An array of characters which should not be decoded. For example,
webmaster@1 329 * array('<', '&', '"'). This affects both named and numerical entities.
webmaster@1 330 */
webmaster@1 331 function decode_entities($text, $exclude = array()) {
webmaster@1 332 static $table;
webmaster@1 333 // We store named entities in a table for quick processing.
webmaster@1 334 if (!isset($table)) {
webmaster@1 335 // Get all named HTML entities.
webmaster@1 336 $table = array_flip(get_html_translation_table(HTML_ENTITIES));
webmaster@1 337 // PHP gives us ISO-8859-1 data, we need UTF-8.
webmaster@1 338 $table = array_map('utf8_encode', $table);
webmaster@1 339 // Add apostrophe (XML)
webmaster@1 340 $table['&apos;'] = "'";
webmaster@1 341 }
webmaster@1 342 $newtable = array_diff($table, $exclude);
webmaster@1 343
webmaster@1 344 // Use a regexp to select all entities in one pass, to avoid decoding double-escaped entities twice.
webmaster@1 345 return preg_replace('/&(#x?)?([A-Za-z0-9]+);/e', '_decode_entities("$1", "$2", "$0", $newtable, $exclude)', $text);
webmaster@1 346 }
webmaster@1 347
webmaster@1 348 /**
webmaster@1 349 * Helper function for decode_entities
webmaster@1 350 */
webmaster@1 351 function _decode_entities($prefix, $codepoint, $original, &$table, &$exclude) {
webmaster@1 352 // Named entity
webmaster@1 353 if (!$prefix) {
webmaster@1 354 if (isset($table[$original])) {
webmaster@1 355 return $table[$original];
webmaster@1 356 }
webmaster@1 357 else {
webmaster@1 358 return $original;
webmaster@1 359 }
webmaster@1 360 }
webmaster@1 361 // Hexadecimal numerical entity
webmaster@1 362 if ($prefix == '#x') {
webmaster@1 363 $codepoint = base_convert($codepoint, 16, 10);
webmaster@1 364 }
webmaster@1 365 // Decimal numerical entity (strip leading zeros to avoid PHP octal notation)
webmaster@1 366 else {
webmaster@1 367 $codepoint = preg_replace('/^0+/', '', $codepoint);
webmaster@1 368 }
webmaster@1 369 // Encode codepoint as UTF-8 bytes
webmaster@1 370 if ($codepoint < 0x80) {
webmaster@1 371 $str = chr($codepoint);
webmaster@1 372 }
webmaster@1 373 else if ($codepoint < 0x800) {
webmaster@1 374 $str = chr(0xC0 | ($codepoint >> 6))
webmaster@1 375 . chr(0x80 | ($codepoint & 0x3F));
webmaster@1 376 }
webmaster@1 377 else if ($codepoint < 0x10000) {
webmaster@1 378 $str = chr(0xE0 | ( $codepoint >> 12))
webmaster@1 379 . chr(0x80 | (($codepoint >> 6) & 0x3F))
webmaster@1 380 . chr(0x80 | ( $codepoint & 0x3F));
webmaster@1 381 }
webmaster@1 382 else if ($codepoint < 0x200000) {
webmaster@1 383 $str = chr(0xF0 | ( $codepoint >> 18))
webmaster@1 384 . chr(0x80 | (($codepoint >> 12) & 0x3F))
webmaster@1 385 . chr(0x80 | (($codepoint >> 6) & 0x3F))
webmaster@1 386 . chr(0x80 | ( $codepoint & 0x3F));
webmaster@1 387 }
webmaster@1 388 // Check for excluded characters
webmaster@1 389 if (in_array($str, $exclude)) {
webmaster@1 390 return $original;
webmaster@1 391 }
webmaster@1 392 else {
webmaster@1 393 return $str;
webmaster@1 394 }
webmaster@1 395 }
webmaster@1 396
webmaster@1 397 /**
webmaster@1 398 * Count the amount of characters in a UTF-8 string. This is less than or
webmaster@1 399 * equal to the byte count.
webmaster@1 400 */
webmaster@1 401 function drupal_strlen($text) {
webmaster@1 402 global $multibyte;
webmaster@1 403 if ($multibyte == UNICODE_MULTIBYTE) {
webmaster@1 404 return mb_strlen($text);
webmaster@1 405 }
webmaster@1 406 else {
webmaster@1 407 // Do not count UTF-8 continuation bytes.
webmaster@1 408 return strlen(preg_replace("/[\x80-\xBF]/", '', $text));
webmaster@1 409 }
webmaster@1 410 }
webmaster@1 411
webmaster@1 412 /**
webmaster@1 413 * Uppercase a UTF-8 string.
webmaster@1 414 */
webmaster@1 415 function drupal_strtoupper($text) {
webmaster@1 416 global $multibyte;
webmaster@1 417 if ($multibyte == UNICODE_MULTIBYTE) {
webmaster@1 418 return mb_strtoupper($text);
webmaster@1 419 }
webmaster@1 420 else {
webmaster@1 421 // Use C-locale for ASCII-only uppercase
webmaster@1 422 $text = strtoupper($text);
webmaster@1 423 // Case flip Latin-1 accented letters
webmaster@1 424 $text = preg_replace_callback('/\xC3[\xA0-\xB6\xB8-\xBE]/', '_unicode_caseflip', $text);
webmaster@1 425 return $text;
webmaster@1 426 }
webmaster@1 427 }
webmaster@1 428
webmaster@1 429 /**
webmaster@1 430 * Lowercase a UTF-8 string.
webmaster@1 431 */
webmaster@1 432 function drupal_strtolower($text) {
webmaster@1 433 global $multibyte;
webmaster@1 434 if ($multibyte == UNICODE_MULTIBYTE) {
webmaster@1 435 return mb_strtolower($text);
webmaster@1 436 }
webmaster@1 437 else {
webmaster@1 438 // Use C-locale for ASCII-only lowercase
webmaster@1 439 $text = strtolower($text);
webmaster@1 440 // Case flip Latin-1 accented letters
webmaster@1 441 $text = preg_replace_callback('/\xC3[\x80-\x96\x98-\x9E]/', '_unicode_caseflip', $text);
webmaster@1 442 return $text;
webmaster@1 443 }
webmaster@1 444 }
webmaster@1 445
webmaster@1 446 /**
webmaster@1 447 * Helper function for case conversion of Latin-1.
webmaster@1 448 * Used for flipping U+C0-U+DE to U+E0-U+FD and back.
webmaster@1 449 */
webmaster@1 450 function _unicode_caseflip($matches) {
webmaster@1 451 return $matches[0][0] . chr(ord($matches[0][1]) ^ 32);
webmaster@1 452 }
webmaster@1 453
webmaster@1 454 /**
webmaster@1 455 * Capitalize the first letter of a UTF-8 string.
webmaster@1 456 */
webmaster@1 457 function drupal_ucfirst($text) {
webmaster@1 458 // Note: no mbstring equivalent!
webmaster@1 459 return drupal_strtoupper(drupal_substr($text, 0, 1)) . drupal_substr($text, 1);
webmaster@1 460 }
webmaster@1 461
webmaster@1 462 /**
webmaster@1 463 * Cut off a piece of a string based on character indices and counts. Follows
webmaster@1 464 * the same behavior as PHP's own substr() function.
webmaster@1 465 *
webmaster@1 466 * Note that for cutting off a string at a known character/substring
webmaster@1 467 * location, the usage of PHP's normal strpos/substr is safe and
webmaster@1 468 * much faster.
webmaster@1 469 */
webmaster@1 470 function drupal_substr($text, $start, $length = NULL) {
webmaster@1 471 global $multibyte;
webmaster@1 472 if ($multibyte == UNICODE_MULTIBYTE) {
webmaster@1 473 return $length === NULL ? mb_substr($text, $start) : mb_substr($text, $start, $length);
webmaster@1 474 }
webmaster@1 475 else {
webmaster@1 476 $strlen = strlen($text);
webmaster@1 477 // Find the starting byte offset
webmaster@1 478 $bytes = 0;
webmaster@1 479 if ($start > 0) {
webmaster@1 480 // Count all the continuation bytes from the start until we have found
webmaster@1 481 // $start characters
webmaster@1 482 $bytes = -1; $chars = -1;
webmaster@1 483 while ($bytes < $strlen && $chars < $start) {
webmaster@1 484 $bytes++;
webmaster@1 485 $c = ord($text[$bytes]);
webmaster@1 486 if ($c < 0x80 || $c >= 0xC0) {
webmaster@1 487 $chars++;
webmaster@1 488 }
webmaster@1 489 }
webmaster@1 490 }
webmaster@1 491 else if ($start < 0) {
webmaster@1 492 // Count all the continuation bytes from the end until we have found
webmaster@1 493 // abs($start) characters
webmaster@1 494 $start = abs($start);
webmaster@1 495 $bytes = $strlen; $chars = 0;
webmaster@1 496 while ($bytes > 0 && $chars < $start) {
webmaster@1 497 $bytes--;
webmaster@1 498 $c = ord($text[$bytes]);
webmaster@1 499 if ($c < 0x80 || $c >= 0xC0) {
webmaster@1 500 $chars++;
webmaster@1 501 }
webmaster@1 502 }
webmaster@1 503 }
webmaster@1 504 $istart = $bytes;
webmaster@1 505
webmaster@1 506 // Find the ending byte offset
webmaster@1 507 if ($length === NULL) {
webmaster@1 508 $bytes = $strlen - 1;
webmaster@1 509 }
webmaster@1 510 else if ($length > 0) {
webmaster@1 511 // Count all the continuation bytes from the starting index until we have
webmaster@1 512 // found $length + 1 characters. Then backtrack one byte.
webmaster@1 513 $bytes = $istart; $chars = 0;
webmaster@1 514 while ($bytes < $strlen && $chars < $length) {
webmaster@1 515 $bytes++;
webmaster@1 516 $c = ord($text[$bytes]);
webmaster@1 517 if ($c < 0x80 || $c >= 0xC0) {
webmaster@1 518 $chars++;
webmaster@1 519 }
webmaster@1 520 }
webmaster@1 521 $bytes--;
webmaster@1 522 }
webmaster@1 523 else if ($length < 0) {
webmaster@1 524 // Count all the continuation bytes from the end until we have found
webmaster@1 525 // abs($length) characters
webmaster@1 526 $length = abs($length);
webmaster@1 527 $bytes = $strlen - 1; $chars = 0;
webmaster@1 528 while ($bytes >= 0 && $chars < $length) {
webmaster@1 529 $c = ord($text[$bytes]);
webmaster@1 530 if ($c < 0x80 || $c >= 0xC0) {
webmaster@1 531 $chars++;
webmaster@1 532 }
webmaster@1 533 $bytes--;
webmaster@1 534 }
webmaster@1 535 }
webmaster@1 536 $iend = $bytes;
webmaster@1 537
webmaster@1 538 return substr($text, $istart, max(0, $iend - $istart + 1));
webmaster@1 539 }
webmaster@1 540 }
webmaster@1 541
webmaster@1 542