Reputation: 571
I want to convert a user input string into url friendly slug in my local language.
I have used laravel 5.2 and tried to use str_slug($request->input('title'))
.
It can convert english string to slug but for local language it returns empty string. My input will be in Bangla language.
Using $request->input('title')
. I get bangla text but could not convert it into url friendly slug.
How can I solve this?
Thanks.
Upvotes: 2
Views: 5203
Reputation: 15
You can try this
<input type="text" name="title" id="title">
<input type="hidden" name="slug" id="slug">
$("#title").keyup(function(){
var str = $(this).val();
var txt = str.replace(/ /g,"-");
$("#slug").val(txt.toLowerCase());
})
this will be produce such type result "আমার-সোনার-বাংলা" of "আমার সোনার বাংলা"
Upvotes: 0
Reputation: 687
You can do this using javascript. Suppose your text is "আমার সোনার বাংলা", the function will return "আমার-সোনার-বাংলা". It also removes multiple dashes.
function slugify(text) {
return text.toLowerCase().replace(text, text).replace(/^-+|-+$/g, '')
.replace(/\s/g, '-').replace(/\-\-+/g, '-');
}
Modified answer from @Salman Mahmud
Upvotes: 0
Reputation: 1322
i know this is old but you can use laravel Str::slug() helper
first parameter is the title
second parameter is the separator
third parameter is the language
example :
Str::slug($file->getClientOriginalName(), '-', 'bn');
Upvotes: 1
Reputation: 571
I have done this using jquery and it's working fine
$('input[name=title]').on('blur', function () {
var slugElm = $('input[name=slug]');
if (slugElm.val()) { return; }
// slugElm.val(this.value.toLowerCase().replace(/[^a-z0-9-]+/g, '-').replace(/^-+|-+$/g, ''));
slugElm.val(this.value.toLowerCase().replace(this.value, this.value).replace(/^-+|-+$/g, '')
.replace(/\s/g, '-'));
})
And there is a solution for laravel.
http://killerwhalesoft.com/blog/make-laravel-slug-support-utf8-characters/
Upvotes: 0
Reputation: 1316
You can use Following Function. I don't know it will work with local language or not. But you can try it.
public function createSlug($str, $delimiter = '-'){
$slug = strtolower(trim(preg_replace('/[\s-]+/', $delimiter, preg_replace('/[^A-Za-z0-9-]+/', $delimiter, preg_replace('/[&]/', 'and', preg_replace('/[\']/', '', iconv('UTF-8', 'ASCII//TRANSLIT', $str))))), $delimiter));
return $slug;
}
OR
You Can see here for your specific local language http://code.google.com/p/php-slugs/
Upvotes: 3