Skip to content

Commit 4c5a6b4

Browse files
authored
Transliteration for filenames in File::makeSafe() (#55)
1 parent 5f30315 commit 4c5a6b4

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

Tests/FileTest.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,22 @@ public function dataTestMakeSafe(): \Generator
102102
'.gitignore',
103103
'Files starting with a fullstop should be allowed when strip chars parameter is empty',
104104
];
105+
106+
if (function_exists('transliterator_transliterate') && function_exists('iconv')) {
107+
yield [
108+
'Änderüng_âsceñt.txt',
109+
[],
110+
'Anderung_ascent.txt',
111+
'Files with non-ascii characters should be transliterated',
112+
];
113+
} else {
114+
yield [
115+
'Änderüng_âsceñt.txt',
116+
[],
117+
'nderng_scet.txt',
118+
'Files with non-ascii characters should be removed when transliteration is not possible',
119+
];
120+
}
105121
}
106122

107123
/**
@@ -117,7 +133,7 @@ public function dataTestMakeSafe(): \Generator
117133
*/
118134
public function testMakeSafe($name, $stripChars, $expected, $message)
119135
{
120-
$this->assertEquals(File::makeSafe($name, $stripChars), $expected, $message);
136+
$this->assertEquals($expected, File::makeSafe($name, $stripChars), $message);
121137
}
122138

123139
/**

src/File.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,19 @@ public static function stripExt($file)
4444
*/
4545
public static function makeSafe($file, array $stripChars = ['#^\.#'])
4646
{
47-
$regex = array_merge(['#(\.){2,}#', '#[^A-Za-z0-9\.\_\- ]#'], $stripChars);
47+
// Try transliterating the file name using the native php function
48+
if (function_exists('transliterator_transliterate') && function_exists('iconv')) {
49+
// Using iconv to ignore characters that can't be transliterated
50+
$file = iconv("UTF-8", "ASCII//TRANSLIT//IGNORE", transliterator_transliterate('Any-Latin; Latin-ASCII', $file));
51+
}
4852

49-
$file = preg_replace($regex, '', $file);
53+
$regex = array_merge(['#(\.){2,}#', '#[^A-Za-z0-9\.\_\- ]#'], $stripChars);
54+
$file = preg_replace($regex, '', $file);
5055

5156
// Remove any trailing dots, as those aren't ever valid file names.
5257
$file = rtrim($file, '.');
5358

54-
return $file;
59+
return trim($file);
5560
}
5661

5762
/**

0 commit comments

Comments
 (0)