I use Str::slug
to generate friendly URL's, however Str::slug()
method returns null
on arabic and hindi strings. Probably chinese, japanese, korean and those charsets too.
For example:
return Str::slug('मनोरंजन'); //null
How can I solve this issue efficiently?
I have faced this problem when I was working with Arabic language, so I've made the following function which solved the problem for me.
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 keeps latin characters and 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;
}
Note that this function solves the problem only for Arabic language, if you want to solve the problem for Hindi or any other language, you need to add Hindi characters (or the other language's characters) beside or instead of these ءاأإآؤئبتثجحخدذرزسشصضطظعغفقكلمنهويةى
existing Arabic characters.
try this:
Save:
Str::slug(Input::get('title'))==""?strtolower(urlencode(Input::get('title'))):Str::slug(Input::get('title'));
Get:
$slug = strtolower(urlencode($slug));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With