Skip to content

Instantly share code, notes, and snippets.

@AmrMekkawy
Last active August 29, 2015 14:13
Show Gist options
  • Save AmrMekkawy/afcc8354fe662c3495b7 to your computer and use it in GitHub Desktop.
Save AmrMekkawy/afcc8354fe662c3495b7 to your computer and use it in GitHub Desktop.
How to make a slug or a friendly URL that contains Arabic characters?
/**
* make_slug
* @author @AmrMekkawy
*
* based on http://goo.gl/7WULfc & http://goo.gl/P6j345
* and modified by @AmrMekkawy to be used with Arabic language
*/
if (!function_exists("make_slug")) {
function make_slug($string = null, $separator = "-") {
if (is_null($string)) {
return "";
}
// Remove spaces from the beginning and from the end of the string
$string = trim($string);
// Lower case everything
// using mb_strtolower() function is important for non-Latin UTF-8 string | more info: http://goo.gl/QL2tzK
$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 has been modified to keep arabic charactrs as well
$string = preg_replace("/[^a-z0-9_\s-ءاأإآؤئبتثجحخدذرزسشصضطظعغفقكلمنهويةى]/u", "", $string);
// Remove multiple dashes or whitespaces
$string = preg_replace("/[\s-]+/", " ", $string);
// Convert whitespaces and underscore to the given separator
$string = preg_replace("/[\s_]/", $separator, $string);
return $string;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment