return substr($string, 0, $len);
}
// Scan backwards to beginning of the byte sequence.
- while (--$len >= 0 && ord($string[$len]) >= 0x80 && ord($string[$len]) < 0xC0);
+ // @todo Make the code more readable in https://www.drupal.org/node/2911497.
+ while (--$len >= 0 && ord($string[$len]) >= 0x80 && ord($string[$len]) < 0xC0) {
+ }
return substr($string, 0, $len);
}
*/
public static function ucwords($text) {
$regex = '/(^|[' . static::PREG_CLASS_WORD_BOUNDARY . '])([^' . static::PREG_CLASS_WORD_BOUNDARY . '])/u';
- return preg_replace_callback($regex, function(array $matches) {
+ return preg_replace_callback($regex, function (array $matches) {
return $matches[1] . Unicode::strtoupper($matches[2]);
}, $text);
}
* Returns < 0 if $str1 is less than $str2; > 0 if $str1 is greater than
* $str2, and 0 if they are equal.
*/
- public static function strcasecmp($str1 , $str2) {
+ public static function strcasecmp($str1, $str2) {
return strcmp(static::strtoupper($str1), static::strtoupper($str2));
}
*
* @param string $string
* The header to encode.
+ * @param bool $shorten
+ * If TRUE, only return the first chunk of a multi-chunk encoded string.
*
* @return string
* The mime-encoded header.
*/
- public static function mimeHeaderEncode($string) {
+ public static function mimeHeaderEncode($string, $shorten = FALSE) {
if (preg_match('/[^\x20-\x7E]/', $string)) {
- $chunk_size = 47; // floor((75 - strlen("=?UTF-8?B??=")) * 0.75);
+ // floor((75 - strlen("=?UTF-8?B??=")) * 0.75);
+ $chunk_size = 47;
$len = strlen($string);
$output = '';
while ($len > 0) {
$chunk = static::truncateBytes($string, $chunk_size);
$output .= ' =?UTF-8?B?' . base64_encode($chunk) . "?=\n";
+ if ($shorten) {
+ break;
+ }
$c = strlen($chunk);
$string = substr($string, $c);
$len -= $c;