2

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.

5 Answers 5

3

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/

1

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');
0

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/

1
  • While there are multiple spaces it returns multiple dashes. something like 'my---name-is'. Apr 16, 2020 at 21:42
0

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

0

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 "আমার সোনার বাংলা"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.