Akash Khan Akash Khan - 1 year ago 170
PHP Question

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)

Answer Source

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: বাংলাদেশ-ব্যাংকের-রিজার্ভের-অর্থ-চুরির-ঘটনায়-ফিলিপাইনের
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download