• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Slugify in Code Igniter

#1
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
 
   $text=strip_tags($text);
 
   // Replace non letter or digits by -
 
   $text preg_replace('~[^\pL\d]+~u''-'$text);
 
   // Transliterate
 
   setlocale(LC_ALL'en_US.utf8');
 
   $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.
Reply


Messages In This Thread
Slugify in Code Igniter - by imabot - 11-02-2018, 03:05 PM
RE: Slugify in Code Igniter - by jreklund - 11-02-2018, 03:58 PM

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.