STRING::ESCAPE_FILENAME
1g 2024-04-29 laplante@plcb.ca GOWEB/STRINGtitle: “STRING::ESCAPE_FILENAME” version: “1.0.0” date: 2024-04-29 author: “laplante@plcb.ca” section: “1g” category: “GOWEBstring”
astring::escape_filename Escape Special Characters in Filenames
string::escape_filename(string [, double: false, char: "-", keep_dir: false, keep_ext: true])
escape_filename(string [, double: false, char: "-", keep_dir: false, keep_ext: true])
string::escapeFilename(string [, double: false, char: "-", keep_dir: false, keep_ext: true])
escapeFilename(string [, double: false, char: "-", keep_dir: false, keep_ext: true])
The escape_filename
function replaces special characters in a string to produce a safe filename.
By default, it retains the characters:
a-z
A-Z
0-9
-
_
Other characters are replaced according to the following mapping:
à
, á
, â
, ä
, ã
, å
→ a
À
, Á
, Â
, Ä
, Ã
, Å
→ A
æ
→ ae
Æ
→ AE
ç
→ c
Ç
→ C
é
, ê
, è
, ë
→ e
É
, Ê
, È
, Ë
→ E
ì
, í
, î
, ï
→ i
Ì
, Í
, Î
, Ï
→ I
Ð
→ D
ð
→ d
Ñ
→ N
ñ
→ n
Ò
, Ó
, Ô
, Õ
, Ö
, Ø
→ O
ò
, ó
, ô
, õ
, ö
, ø
→ o
×
→ x
Ù
, Ú
, Û
, Ü
→ U
ù
, ú
, û
, ü
→ u
Ý
, Ÿ
→ Y
ý
, ÿ
→ y
Þ
→ B
þ
→ b
double (boolean, optional)
If false
, when a character is replaced, only a single replacement character is inserted, even if multiple special characters occur consecutively.
char (string, optional)
The character used to replace special characters. Default is "-"
.
keep_ext (boolean, optional)
If true
, the period (.
) character is preserved for file extensions (e.g., resolv.conf
).
keep_dir (boolean, optional)
If true
, the slash (/
) character is preserved for directory paths.
res={{
escape_filename("ééaa..$$##!!.xyz");
escape_filename("
'à', 'á', 'â', 'ä', 'ã', 'å':
'À', 'Á', 'Â', 'Ä', 'Ã', 'Å':
'æ':
'Æ':
'ç':
'Ç':
'é', 'ê', 'è', 'ë':
'É', 'Ê', 'È', 'Ë':
'ì', 'í', 'î', 'ï':
'Ì', 'Í', 'Î', 'Ï':
'Ð':
'ð':
'Ñ':
'ñ':
'Ò', 'Ó', 'Ô', 'Õ', 'Ö', 'Ø':
'ò', 'ó', 'ô', 'õ', 'ö', 'ø':
'×':
'Ù', 'Ú', 'Û', 'Ü':
'ù', 'ú', 'û', 'ü':
'Ý', 'Ÿ':
'ý', 'ÿ':
'Þ':
'þ':");
}};
Returns:
res = eeaa-xyz-a-a-a-a-a-a-A-A-A-A-A-A-ae-AE-c-C-e-e-e-e-E-E-E-E-i-i-i-i-I-I-I-I-D-d-N-n-O-O-O-O-O-O-o-o-o-o-o-o-x-U-U-U-U-u-u-u-u-Y-Y-y-y-B-b-.