CodeIgniter Forums
Slugify in Code Igniter - Printable Version

+- CodeIgniter Forums (
+-- Forum: Development (
+--- Forum: CodeIgniter 3.x (
+--- Thread: Slugify in Code Igniter (/showthread.php?tid=72085)

Slugify in Code Igniter - imabot - 11-02-2018

Slugifying a string is a real pain in the a**  with CodeIgniter (accented characters, apostrophes ... there is always something wrong in the slug)

Why not adding the following function once and for all in url_helper.php ?

PHP Code:
// Slugify a string
function slugify($text)
   // Strip html tags
   // Replace non letter or digits by -
   $text preg_replace('~[^\pL\d]+~u''-'$text);
   // Transliterate
   $text iconv('utf-8''us-ascii//TRANSLIT'$text);
   // Remove unwanted characters
   $text preg_replace('~[^-\w]+~'''$text);
   // Trim
   $text trim($text'-');
   // Remove duplicate -
   $text preg_replace('~-+~''-'$text);
   // Lowercase
   $text strtolower($text);
   // Check if it is empty
   if (empty($text)) { return 'n-a'; }
   // Return result
   return $text;

I'm convinced this will help many many many developers around the world.

RE: Slugify in Code Igniter - jreklund - 11-02-2018

I'm afraid there are several reason that I think it won't be implemented. Here's a few.
1. You are removing non letters and digits twice.
~[^\pL\d]+~u and [^-\w]+
- This one will remove also "Help"-button, so it becomes Helpbutton, that's not what I wanted. But help-button
2. Setlocale will affect the rest of the code too. And other people on the same process.
3. Forcing a convert with US standard. Every server should have that charset installed. But no grantee.
4. Forcing a value upon a user.
5. No options, you can't change the separator or leave it out.

You can use this one if you want. It uses PHP internal Transliterator Class.