Last active
April 29, 2024 08:36
-
-
Save hishamdalal/adb99df8c115479c4298cead043b2dbc to your computer and use it in GitHub Desktop.
PHP clean and slugify filename (Allow just english and arabic characters for file and folder name)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// https://stackoverflow.com/a/2021647/2269902 | |
// https://stackoverflow.com/a/56328226/2269902 | |
// https://graphemica.com/unicode/characters | |
// arabic characters unicode range (0600–06FF, 256 characters) | |
function slugify($file_name) { | |
$unicode = [ | |
"~[\x{0000}-\x{001F}]~u", # control characters | |
"~[\x{0021}-\x{0023}]~u", # ! " # | |
"~[\x{0027}]~u", # ' | |
"~[\x{002A}]~u", # * | |
"~[\x{002C}]~u", # , | |
"~[\x{002F}]~u", # / | |
"~[\x{003A}-\x{003C}]~u", # : ; < | |
"~[\x{003E}-\x{0040}]~u", # > ? @ | |
"~[\x{005C}]~u", # \ | |
"~[\x{005E}]~u", # ^ | |
"~[\x{0060}]~u", # ` | |
"~[\x{007C}]~u", # | | |
"~[\x{007F}-\x{00BF}]~u", | |
"~[\x{00C0}-\x{05F4}]~u", | |
"~[\x{0600}-\x{0620}]~u", # -> ؠ | |
"~[\x{063B}-\x{063F}]~u", # ػ -> ؿ | |
"~[\x{064B}-\x{0652}]~u", # arabic diacritic | |
"~[\x{0656}-\x{065F}]~u", # arabic diacritic | |
"~[\x{066B}-\x{0670}]~u", # , ... | |
"~[\x{0675}-\x{2EBE0}]~u", | |
// "~[\x{0700}-\x{2EBE0}]~u", | |
"/-+/", | |
]; | |
$file_name = preg_replace($unicode, '', $file_name); | |
$file_name = preg_replace('/-+/', '-', $file_name); | |
$file_name = trim($file_name, '-'); | |
$file_name = trim($file_name, ' '); | |
$file_name = str_replace(' ', ' ', $file_name); | |
return $file_name; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Clean and slugify filename
Allow only arabic & english letters and some symbols.