4

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.

  • if i remember correctly, UTF-8 doesnt support japanese fully... you have to use UNICODE – RG Servers Jul 29 '20 at 08:17
  • do not use normal `str*` functions on multi-byte strings because these only work on ASCII strings and will probably break multi-byte strings. Use the [`mb_*` equivalents](https://www.php.net/manual/en/ref.mbstring.php). Also do not use `utf8_encode` because generally speaking it will not work unless you already have `ISO-8859-1` as the input string which is highly unlikely if you have Japanese characters – apokryfos Jul 29 '20 at 08:27
  • @apokryfos will definitely consider that, thanks –  Jul 29 '20 at 09:56

1 Answers1

1

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');
apokryfos
  • 38,771
  • 9
  • 70
  • 114
  • Thank you, It did work but as you said it converts a string to ASCII. For the title =>サービス登録 サービス登録 ... it gives "sabisudeng-lu-sabisudeng-lu" but for SEO purpose we would want it to be "サービス登露-サービス登露" . So I think creating the custom function is the best option. –  Jul 29 '20 at 09:48