sanitize_title_with_dashes

Sanitizes a title, replacing whitespace and a few other characters with dashes.

Description

(string) sanitize_title_with_dashes( (string) $title, (string) $raw_title = '', (string) $context = 'display' ); 

Limits the output to alphanumeric characters, underscore (_) and dash (-). Whitespace becomes a dash.

Returns (string)

The sanitized title.

Parameters (3)

0. $title (string)
The title to be sanitized.
1. $raw_title — Optional. (string) => ''
Not used.
2. $context — Optional. (string) => 'display'
The operation for which the string is sanitized.

Usage

  1. if ( !function_exists( 'sanitize_title_with_dashes' ) ) { 
  2. require_once ABSPATH . WPINC . '/formatting.php'; 
  3.  
  4. // The title to be sanitized. 
  5. $title = ''; 
  6.  
  7. // Optional. Not used. 
  8. $raw_title = ''; 
  9.  
  10. // Optional. The operation for which the string is sanitized. 
  11. $context = 'display'; 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = sanitize_title_with_dashes($title, $raw_title, $context); 
  15.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/formatting.php  
  1. function sanitize_title_with_dashes( $title, $raw_title = '', $context = 'display' ) { 
  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. $title = utf8_uri_encode($title, 200); 
  14.  
  15. $title = strtolower($title); 
  16.  
  17. if ( 'save' == $context ) { 
  18. // Convert nbsp, ndash and mdash to hyphens 
  19. $title = str_replace( array( '%c2%a0', '%e2%80%93', '%e2%80%94' ), '-', $title ); 
  20. // Convert nbsp, ndash and mdash HTML entities to hyphens 
  21. $title = str_replace( array( ' ', ' ', '–', '–', '—', '—' ), '-', $title ); 
  22.  
  23. // Strip these characters entirely 
  24. $title = str_replace( array( 
  25. // iexcl and iquest 
  26. '%c2%a1', '%c2%bf',  
  27. // angle quotes 
  28. '%c2%ab', '%c2%bb', '%e2%80%b9', '%e2%80%ba',  
  29. // curly quotes 
  30. '%e2%80%98', '%e2%80%99', '%e2%80%9c', '%e2%80%9d',  
  31. '%e2%80%9a', '%e2%80%9b', '%e2%80%9e', '%e2%80%9f',  
  32. // copy, reg, deg, hellip and trade 
  33. '%c2%a9', '%c2%ae', '%c2%b0', '%e2%80%a6', '%e2%84%a2',  
  34. // acute accents 
  35. '%c2%b4', '%cb%8a', '%cc%81', '%cd%81',  
  36. // grave accent, macron, caron 
  37. '%cc%80', '%cc%84', '%cc%8c',  
  38. ), '', $title ); 
  39.  
  40. // Convert times to x 
  41. $title = str_replace( '%c3%97', 'x', $title ); 
  42.  
  43. $title = preg_replace('/&.+?;/', '', $title); // kill entities 
  44. $title = str_replace('.', '-', $title); 
  45.  
  46. $title = preg_replace('/[^%a-z0-9 _-]/', '', $title); 
  47. $title = preg_replace('/\s+/', '-', $title); 
  48. $title = preg_replace('|-+|', '-', $title); 
  49. $title = trim($title, '-'); 
  50.  
  51. return $title;