Advertisement
dslatten

WP Permalink Sanitizer

Sep 23rd, 2012
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.34 KB | None | 0 0
  1. /**
  2.  * Sanitizes title, replacing whitespace and a few other characters with dashes.
  3.  *
  4.  * Limits the output to alphanumeric characters, underscore (_) and dash (-).
  5.  * Whitespace becomes a dash.
  6.  *
  7.  * @since 1.2.0
  8.  *
  9.  * @param string $title The title to be sanitized.
  10.  * @param string $raw_title Optional. Not used.
  11.  * @param string $context Optional. The operation for which the string is sanitized.
  12.  * @return string The sanitized title.
  13.  */
  14. function sanitize_title_with_dashes($title, $raw_title = '', $context = 'display') {
  15.     $title = strip_tags($title);
  16.     // Preserve escaped octets.
  17.     $title = preg_replace('|%([a-fA-F0-9][a-fA-F0-9])|', '---$1---', $title);
  18.     // Remove percent signs that are not part of an octet.
  19.     $title = str_replace('%', '', $title);
  20.     // Restore octets.
  21.     $title = preg_replace('|---([a-fA-F0-9][a-fA-F0-9])---|', '%$1', $title);
  22.  
  23.     if (seems_utf8($title)) {
  24.             if (function_exists('mb_strtolower')) {
  25.                     $title = mb_strtolower($title, 'UTF-8');
  26.             }
  27.             $title = utf8_uri_encode($title, 200);
  28.     }
  29.  
  30.     $title = strtolower($title);
  31.     $title = preg_replace('/&.+?;/', '', $title); // kill entities
  32.     $title = str_replace('.', '-', $title);
  33.  
  34.     if ( 'save' == $context ) {
  35.             // Convert nbsp, ndash and mdash to hyphens
  36.             $title = str_replace( array( '%c2%a0', '%e2%80%93', '%e2%80%94' ), '-', $title );
  37.  
  38.             // Strip these characters entirely
  39.             $title = str_replace( array(
  40.                     // iexcl and iquest
  41.                     '%c2%a1', '%c2%bf',
  42.                     // angle quotes
  43.                     '%c2%ab', '%c2%bb', '%e2%80%b9', '%e2%80%ba',
  44.                     // curly quotes
  45.                     '%e2%80%98', '%e2%80%99', '%e2%80%9c', '%e2%80%9d',
  46.                     '%e2%80%9a', '%e2%80%9b', '%e2%80%9e', '%e2%80%9f',
  47.                     // copy, reg, deg, hellip and trade
  48.                     '%c2%a9', '%c2%ae', '%c2%b0', '%e2%80%a6', '%e2%84%a2',
  49.             ), '', $title );
  50.  
  51.             // Convert times to x
  52.             $title = str_replace( '%c3%97', 'x', $title );
  53.     }
  54.  
  55.     $title = preg_replace('/[^%a-z0-9 _-]/', '', $title);
  56.     $title = preg_replace('/\s+/', '-', $title);
  57.     $title = preg_replace('|-+|', '-', $title);
  58.     $title = trim($title, '-');
  59.  
  60.     return $title;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement