Advertisement
slaFFik

Sanitize Title

Aug 8th, 2011
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.87 KB | None | 0 0
  1. function sanitize_title_with_dashes($title) {
  2.     $title = strip_tags($title);
  3.     // Preserve escaped octets.
  4.     $title = preg_replace('|%([a-fA-F0-9][a-fA-F0-9])|', '---$1---', $title);
  5.     // Remove percent signs that are not part of an octet.
  6.     $title = str_replace('%', '', $title);
  7.     // Restore octets.
  8.     $title = preg_replace('|---([a-fA-F0-9][a-fA-F0-9])---|', '%$1', $title);
  9.  
  10.     if (seems_utf8($title)) {
  11.         if (function_exists('mb_strtolower')) {
  12.             $title = mb_strtolower($title, 'UTF-8');
  13.         }
  14.         $title = utf8_uri_encode($title, 200);
  15.     }
  16.  
  17.     $title = strtolower($title);
  18.     $title = preg_replace('/&.+?;/', '', $title); // kill entities
  19.     $title = str_replace('.', '-', $title);
  20.     $title = preg_replace('/[^%a-z0-9 _-]/', '', $title);
  21.     $title = preg_replace('/\s+/', '-', $title);
  22.     $title = preg_replace('|-+|', '-', $title);
  23.     $title = trim($title, '-');
  24.  
  25.     return $title;
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement