Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel str_slug not working for unicode bangla

Tags:

php

laravel

I am working in a laravel project. I have slugged url. Its working fine for English language. But while I use Bangla it returns empty. Please help me to solve the issue.

echo str_slug("hi there");

// Result: hi-there

echo  str_slug("বাংলাদেশ ব্যাংকের রিজার্ভের অর্থ চুরির ঘটনায় ফিলিপাইনের");

// Result: '' (Empty)
like image 385
Akash Khan Avatar asked Aug 06 '16 04:08

Akash Khan


1 Answers

str_slug or facade version Str::slug doesn't work with non-ascii string. You can instead use this approach

function make_slug($string) {
    return preg_replace('/\s+/u', '-', trim($string));
}

$slug = make_slug(" বাংলাদেশ   ব্যাংকের    রিজার্ভের  অর্থ  চুরির   ঘটনায়   ফিলিপাইনের  ");
echo $slug;


// Output: বাংলাদেশ-ব্যাংকের-রিজার্ভের-অর্থ-চুরির-ঘটনায়-ফিলিপাইনের
like image 84
Zayn Ali Avatar answered Sep 24 '22 19:09

Zayn Ali