Having Problem in Creating slugs for Japanese language using LARAVEL

Viewed 766

I am creating this app using laravel. It requires to have Japanese slugs because almost all of the content is in Japanese language. I tried to use several packages but none of them provide good support to Japanese language. So, I am trying to create it myself. In order to have proper slug I am trying to achieve these..

  1. strips HTML & PHP
  2. strips special chars
  3. converts all characters to lowercaps
  4. replaces whitespaces, underscores and periods by hyphens/dashes
  5. reduces multiple consecutive dashes to one

To strips special characters I thought of using preg_replace() but the problem is it is also removing the Japanese letters. I tried encoding it to utf8 but no solution. Now, I want to create the function that will replace all the characters not required in a slug.

$slug = iconv("UTF-8", "ISO-8859-1//TRANSLIT", utf8_encode(strtolower((str_replace(' ', '-', $title)))));

So, I want a list/array of characters that must be replaced. I have listed these.If you think any other characters must be considered please help?

array("~", "!", "@","#","$","%","^","&","*","(",")","_","+","}","{","[","]",".",",","\\","/","|");

If you have any alternative solution to this I would love to use that.

1 Answers

Laravel has a string helper to convert a string to ASCII which might help. It is also baked in the slug helper. Try this:

Str::slug($title, '-', 'ja');
Related