I suggest that you add a param to allow user to choose allow ASCII conversion or not.
public static function slug($title, $separator = '-', $enable_ascii=true)
{
if($enable_ascii) {
$title = static::ascii($title);
}
...
Feel free to add your own function to your app. We're intentionally forcing ascii in uris.
I speak 3 languages. For Farsi use this:
function make_slug($string, $separator = '-')
{
$string = trim($string);
$string = mb_strtolower($string, 'UTF-8');
// Make alphanumeric (removes all other characters)
// this makes the string safe especially when used as a part of a URL
// this keeps latin characters and Persian characters as well
$string = preg_replace("/[^a-z0-9_\s-ءاآؤئبپتثجچحخدذرزژسشصضطظعغفقكکگلمنوهی]/u", '', $string);
// Remove multiple dashes or whitespaces or underscores
$string = preg_replace("/[\s-_]+/", ' ', $string);
// Convert whitespaces and underscore to the given separator
$string = preg_replace("/[\s_]/", $separator, $string);
return $string;
}
Thank u very much mr Azimi. I use your code but i added some character for example other unicode of ک ه and farsi numbers.
function make_slug($string, $separator = '-')
{
$string = trim($string);
$string = mb_strtolower($string, 'UTF-8');
// Make alphanumeric (removes all other characters)
// this makes the string safe especially when used as a part of a URL
// this keeps latin characters and Persian characters as well
$string = preg_replace("/[^a-z0-9_\s-۰۱۲۳۴۵۶۷۸۹ءاآؤئبپتثجچحخدذرزژسشصضطظعغفقکكگگلمنوهی]/u", '', $string);
// Remove multiple dashes or whitespaces or underscores
$string = preg_replace("/[\s-_]+/", ' ', $string);
// Convert whitespaces and underscore to the given separator
$string = preg_replace("/[\s_]/", $separator, $string);
return $string;
}
this is amazing
Using @hassanazimi 's solution, you may also want to skip punctuation using the following quick fix:
//...
$arabic_punct = ['،', '؛', '؟', '⠐', '!'];
foreach ($arabic_punct as $punct) {
$string = preg_replace("#".mb_strtolower($punct, 'UTF-8')."#", '', $string);
}
//...
@GrahamCampbell since the main purpose of slugs is SEO this doesn't seem like a wise choice, does it?
Thanks @mhmalekian for your function it actually works great at arabic but I noticed that it strips the " إ , يـ ,ة" characters from the string ,, here is slight update to fix this :
//slugify arabic characters string function
public function make_slug($string, $separator = '-')
{
$string = trim($string);
$string = mb_strtolower($string, 'UTF-8');
// Make alphanumeric (removes all other characters)
// this makes the string safe especially when used as a part of a URL
// this keeps latin characters and Persian characters as well
$string = preg_replace("/[^a-z0-9_\s-۰۱۲۳۴۵۶۷۸۹يـءاآؤئبپتثجچحخدذرزژسشصضطظعغفقکكگگلمنوهیإلأة ]/u", '', $string);
// Remove multiple dashes or whitespaces or underscores
$string = preg_replace("/[\s-_]+/", ' ', $string);
// Convert whitespaces and underscore to the given separator
$string = preg_replace("/[\s_]/", $separator, $string);
return $string;
}
}
preg_match(): Compilation failed: invalid range in character class at offset 11
if you get this error and php version is 7.3 use \- instead of -
[^a-z0-9_\s\-
and
"/[\s\-_]+/"
Thank you all for your contribution.
and special Thanks, @mohammad6006 you made my day.
Most helpful comment
if you get this error and php version is 7.3 use
\-instead of-[^a-z0-9_\s\-and
"/[\s\-_]+/"