function FileEventSubscriber::coerceToValidUtf8
Same name and namespace in other branches
- 11.x core/modules/file/src/EventSubscriber/FileEventSubscriber.php \Drupal\file\EventSubscriber\FileEventSubscriber::coerceToValidUtf8()
Replaces invalid UTF-8 byte sequences in a string.
PHP's mb_convert_encoding() replaces every invalid byte with a ? so any that already present are preserved when a different $unknown_character is specified.
Parameters
string $string: The string to coerce to valid UTF-8.
string $unknown_character: The character substituted for invalid byte sequences.
Return value
string The string as valid UTF-8.
File
-
core/
modules/ file/ src/ EventSubscriber/ FileEventSubscriber.php, line 150
Class
- FileEventSubscriber
- Sanitizes uploaded filenames.
Namespace
Drupal\file\EventSubscriberCode
private static function coerceToValidUtf8(string $string, string $unknown_character = '?') : string {
$code_point = mb_ord($unknown_character, 'UTF-8');
if ($code_point === FALSE) {
throw new \InvalidArgumentException('$unknown_character must be a single valid UTF-8 character.');
}
$previous = mb_substitute_character();
mb_substitute_character($code_point);
$string = mb_convert_encoding($string, 'UTF-8', 'UTF-8');
mb_substitute_character($previous);
return $string;
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.