X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=CRM%2FUtils%2FFile.php;h=4fd3706bfa08e05e91b69546d0517a5db62aa13c;hb=d41a5d539b9fbb6b44b364ac016e7c3d47005c91;hp=0b81f696063898bf579d4eb2962bfbbd62388bad;hpb=08a859c2d466a6f341c025a111ba41698d5083c8;p=civicrm-core.git diff --git a/CRM/Utils/File.php b/CRM/Utils/File.php index 0b81f69606..4fd3706bfa 100644 --- a/CRM/Utils/File.php +++ b/CRM/Utils/File.php @@ -459,6 +459,27 @@ class CRM_Utils_File { } } + /** + * CRM_Utils_String::munge() doesn't handle unicode and needs to be able + * to generate valid database tablenames so will sometimes generate a + * random string. Here what we want is a human-sensible filename that might + * contain unicode. + * Note that this does filter out emojis and such, but keeps characters that + * are considered alphanumeric in non-english languages. + * + * @param string $input + * @param string $replacementString Character or string to replace invalid characters with. Can be the empty string. + * @param int $cutoffLength Length to truncate the result after replacements. + * @return string + */ + public static function makeFilenameWithUnicode(string $input, string $replacementString = '_', int $cutoffLength = 63): string { + $filename = preg_replace('/\W/u', $replacementString, $input); + if ($cutoffLength) { + return mb_substr($filename, 0, $cutoffLength); + } + return $filename; + } + /** * Copies a file *