Skip to content
Advertisement

Laravel str_slug not working for unicode bangla

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)

Advertisement

Answer

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: বাংলাদেশ-ব্যাংকের-রিজার্ভের-অর্থ-চুরির-ঘটনায়-ফিলিপাইনের
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement