Advertisement
Guest User

Untitled

a guest
Feb 26th, 2020
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.61 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Sanitizes a title, replacing whitespace and a few other characters with dashes.
  4.  *
  5.  * Limits the output to alphanumeric characters, underscore (_) and dash (-).
  6.  * Whitespace becomes a dash.
  7.  *
  8.  * @since 1.2.0
  9.  *
  10.  * @param string $title     The title to be sanitized.
  11.  * @param string $raw_title Optional. Not used.
  12.  * @param string $context   Optional. The operation for which the string is sanitized.
  13.  * @return string The sanitized title.
  14.  */
  15. function sanitize_title_with_dashes( $title, $raw_title = '', $context = 'display' ) {
  16.     $title = strip_tags( $title );
  17.     // Preserve escaped octets.
  18.     $title = preg_replace( '|%([a-fA-F0-9][a-fA-F0-9])|', '---$1---', $title );
  19.     // Remove percent signs that are not part of an octet.
  20.     $title = str_replace( '%', '', $title );
  21.     // Restore octets.
  22.     $title = preg_replace( '|---([a-fA-F0-9][a-fA-F0-9])---|', '%$1', $title );
  23.  
  24.     if ( seems_utf8( $title ) ) {
  25.         if ( function_exists( 'mb_strtolower' ) ) {
  26.             $title = mb_strtolower( $title, 'UTF-8' );
  27.         }
  28.         $title = utf8_uri_encode( $title, 200 );
  29.     }
  30.  
  31.     $title = strtolower( $title );
  32.  
  33.     if ( 'save' == $context ) {
  34.         // Convert nbsp, ndash and mdash to hyphens
  35.         $title = str_replace( array( '%c2%a0', '%e2%80%93', '%e2%80%94' ), '-', $title );
  36.         // Convert nbsp, ndash and mdash HTML entities to hyphens
  37.         $title = str_replace( array( '&nbsp;', '&#160;', '&ndash;', '&#8211;', '&mdash;', '&#8212;' ), '-', $title );
  38.         // Convert forward slash to hyphen
  39.         $title = str_replace( '/', '-', $title );
  40.  
  41.         // Strip these characters entirely
  42.         $title = str_replace(
  43.             array(
  44.                 // soft hyphens
  45.                 '%c2%ad',
  46.                 // iexcl and iquest
  47.                 '%c2%a1',
  48.                 '%c2%bf',
  49.                 // angle quotes
  50.                 '%c2%ab',
  51.                 '%c2%bb',
  52.                 '%e2%80%b9',
  53.                 '%e2%80%ba',
  54.                 // curly quotes
  55.                 '%e2%80%98',
  56.                 '%e2%80%99',
  57.                 '%e2%80%9c',
  58.                 '%e2%80%9d',
  59.                 '%e2%80%9a',
  60.                 '%e2%80%9b',
  61.                 '%e2%80%9e',
  62.                 '%e2%80%9f',
  63.                 // copy, reg, deg, hellip and trade
  64.                 '%c2%a9',
  65.                 '%c2%ae',
  66.                 '%c2%b0',
  67.                 '%e2%80%a6',
  68.                 '%e2%84%a2',
  69.                 // acute accents
  70.                 '%c2%b4',
  71.                 '%cb%8a',
  72.                 '%cc%81',
  73.                 '%cd%81',
  74.                 // grave accent, macron, caron
  75.                 '%cc%80',
  76.                 '%cc%84',
  77.                 '%cc%8c',
  78.             ),
  79.             '',
  80.             $title
  81.         );
  82.  
  83.         // Convert times to x
  84.         $title = str_replace( '%c3%97', 'x', $title );
  85.     }
  86.  
  87.     $title = preg_replace( '/&.+?;/', '', $title ); // kill entities
  88.     $title = str_replace( '.', '-', $title );
  89.  
  90.     $title = preg_replace( '/[^%a-z0-9 _-]/', '', $title );
  91.     $title = preg_replace( '/\s+/', '-', $title );
  92.     $title = preg_replace( '|-+|', '-', $title );
  93.     $title = trim( $title, '-' );
  94.  
  95.     return $title;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement