Make WordPress Core

Changeset 1636


Ignore:
Timestamp:
09/10/2004 08:36:54 AM (21 years ago)
Author:
rboren
Message:

Incorporate utf8_uri_encode() in sanitize_title(). Improve UTF-8 accented character decomposition in remove_accents().

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/functions-formatting.php

    r1623 r1636  
    9898}
    9999
     100function utf8_uri_encode( $utf8_string ) {
     101  $unicode = '';       
     102  $values = array();
     103  $num_octets = 1;
     104       
     105  for ($i = 0; $i < strlen( $utf8_string ); $i++ ) {
     106
     107    $value = ord( $utf8_string[ $i ] );
     108           
     109    if ( $value < 128 ) {
     110      $unicode .= chr($value);
     111    } else {
     112      if ( count( $values ) == 0 ) $num_octets = ( $value < 224 ) ? 2 : 3;
     113               
     114      $values[] = $value;
     115     
     116      if ( count( $values ) == $num_octets ) {
     117    if ($num_octets == 3) {
     118      $unicode .= '%' . dechex($values[0]) . '%' . dechex($values[1]) . '%' . dechex($values[2]);
     119    } else {
     120      $unicode .= '%' . dechex($values[0]) . '%' . dechex($values[1]);
     121    }
     122
     123    $values = array();
     124    $num_octets = 1;
     125      }
     126    }
     127  }
     128
     129  return $unicode;   
     130}
     131
    100132function remove_accents($string) {
    101     $chars['in'] = chr(128).chr(131).chr(138).chr(142).chr(154).chr(158)
    102       .chr(159).chr(162).chr(165).chr(181).chr(192).chr(193).chr(194)
    103       .chr(195).chr(196).chr(197).chr(199).chr(200).chr(201).chr(202)
    104       .chr(203).chr(204).chr(205).chr(206).chr(207).chr(209).chr(210)
    105       .chr(211).chr(212).chr(213).chr(214).chr(216).chr(217).chr(218)
    106       .chr(219).chr(220).chr(221).chr(224).chr(225).chr(226).chr(227)
    107       .chr(228).chr(229).chr(231).chr(232).chr(233).chr(234).chr(235)
    108       .chr(236).chr(237).chr(238).chr(239).chr(241).chr(242).chr(243)
    109       .chr(244).chr(245).chr(246).chr(248).chr(249).chr(250).chr(251)
    110       .chr(252).chr(253).chr(255);
    111     $chars['out'] = "EfSZszYcYuAAAAAACEEEEIIIINOOOOOOUUUUYaaaaaaceeeeiiiinoooooouuuuyy";
    112133    if (seems_utf8($string)) {
    113         $invalid_latin_chars = array(chr(197).chr(146) => 'OE', chr(197).chr(147) => 'oe', chr(197).chr(160) => 'S', chr(197).chr(189) => 'Z', chr(197).chr(161) => 's', chr(197).chr(190) => 'z', chr(226).chr(130).chr(172) => 'E');
    114         $string = utf8_decode(strtr($string, $invalid_latin_chars));
    115     }
    116     $string = strtr($string, $chars['in'], $chars['out']);
    117     $double_chars['in'] = array(chr(140), chr(156), chr(198), chr(208), chr(222), chr(223), chr(230), chr(240), chr(254));
    118     $double_chars['out'] = array('OE', 'oe', 'AE', 'DH', 'TH', 'ss', 'ae', 'dh', 'th');
    119     $string = str_replace($double_chars['in'], $double_chars['out'], $string);
     134        $chars = array(chr(195).chr(128) => 'A', chr(195).chr(129) => 'A',
     135                                     chr(195).chr(130) => 'A', chr(195).chr(131) => 'A',
     136                                     chr(195).chr(132) => 'A', chr(195).chr(133) => 'A',
     137                                     chr(195).chr(135) => 'C', chr(195).chr(136) => 'E',
     138                                     chr(195).chr(137) => 'E', chr(195).chr(138) => 'E',
     139                                     chr(195).chr(139) => 'E', chr(195).chr(140) => 'I',
     140                                     chr(195).chr(141) => 'I', chr(195).chr(142) => 'I',
     141                                     chr(195).chr(143) => 'I', chr(195).chr(145) => 'N',
     142                                     chr(195).chr(146) => 'O', chr(195).chr(147) => 'O',
     143                                     chr(195).chr(148) => 'O', chr(195).chr(149) => 'O',
     144                                     chr(195).chr(150) => 'O', chr(195).chr(153) => 'U',
     145                                     chr(195).chr(154) => 'U', chr(195).chr(155) => 'U',
     146                                     chr(195).chr(156) => 'U', chr(195).chr(157) => 'Y',
     147                                     chr(195).chr(160) => 'a', chr(195).chr(161) => 'a',
     148                                     chr(195).chr(162) => 'a', chr(195).chr(163) => 'a',
     149                                     chr(195).chr(164) => 'a', chr(195).chr(165) => 'a',
     150                                     chr(195).chr(167) => 'c', chr(195).chr(168) => 'e',
     151                                     chr(195).chr(169) => 'e', chr(195).chr(170) => 'e',
     152                                     chr(195).chr(171) => 'e', chr(195).chr(172) => 'i',
     153                                     chr(195).chr(173) => 'i', chr(195).chr(174) => 'i',
     154                                     chr(195).chr(175) => 'i', chr(195).chr(177) => 'n',
     155                                     chr(195).chr(178) => 'o', chr(195).chr(179) => 'o',
     156                                     chr(195).chr(180) => 'o', chr(195).chr(181) => 'o',
     157                                     chr(195).chr(182) => 'o', chr(195).chr(182) => 'o',
     158                                     chr(195).chr(185) => 'u', chr(195).chr(186) => 'u',
     159                                     chr(195).chr(187) => 'u', chr(195).chr(188) => 'u',
     160                                     chr(195).chr(189) => 'y', chr(195).chr(191) => 'y',
     161                                     chr(197).chr(146) => 'OE', chr(197).chr(147) => 'oe',
     162                                     chr(197).chr(160) => 'S', chr(197).chr(161) => 's',
     163                                     chr(197).chr(189) => 'Z', chr(197).chr(190) => 'z',
     164                                     chr(226).chr(130).chr(172) => 'E');
     165
     166        $string = strtr($string, $chars);
     167    } else {
     168        // Assume ISO-8859-1 if not UTF-8
     169        $chars['in'] = chr(128).chr(131).chr(138).chr(142).chr(154).chr(158)
     170            .chr(159).chr(162).chr(165).chr(181).chr(192).chr(193).chr(194)
     171            .chr(195).chr(196).chr(197).chr(199).chr(200).chr(201).chr(202)
     172            .chr(203).chr(204).chr(205).chr(206).chr(207).chr(209).chr(210)
     173            .chr(211).chr(212).chr(213).chr(214).chr(216).chr(217).chr(218)
     174            .chr(219).chr(220).chr(221).chr(224).chr(225).chr(226).chr(227)
     175            .chr(228).chr(229).chr(231).chr(232).chr(233).chr(234).chr(235)
     176            .chr(236).chr(237).chr(238).chr(239).chr(241).chr(242).chr(243)
     177            .chr(244).chr(245).chr(246).chr(248).chr(249).chr(250).chr(251)
     178            .chr(252).chr(253).chr(255);
     179
     180        $chars['out'] = "EfSZszYcYuAAAAAACEEEEIIIINOOOOOOUUUUYaaaaaaceeeeiiiinoooooouuuuyy";
     181
     182        $string = strtr($string, $chars['in'], $chars['out']);
     183        $double_chars['in'] = array(chr(140), chr(156), chr(198), chr(208), chr(222), chr(223), chr(230), chr(240), chr(254));
     184        $double_chars['out'] = array('OE', 'oe', 'AE', 'DH', 'TH', 'ss', 'ae', 'dh', 'th');
     185        $string = str_replace($double_chars['in'], $double_chars['out'], $string);
     186    }
     187
    120188    return $string;
    121189}
     
    134202function sanitize_title_with_dashes($title) {
    135203    $title = remove_accents($title);
     204        if (seems_utf8($title)) {
     205            if (function_exists('mb_strtolower')) {
     206                $title = mb_strtolower($title, 'UTF-8');
     207            }
     208            $title = utf8_uri_encode($title);
     209        }
     210
    136211    $title = strtolower($title);
    137212    $title = preg_replace('/&.+?;/', '', $title); // kill entities
    138     $title = preg_replace('/[^a-z0-9 _-]/', '', $title);
    139     $title = preg_replace('/\s+/', ' ', $title);
    140     $title = str_replace(' ', '-', $title);
     213    $title = preg_replace('/[^%a-z0-9 _-]/', '', $title);
     214    $title = preg_replace('/\s+/', '-', $title);
    141215    $title = preg_replace('|-+|', '-', $title);
    142216    $title = trim($title, '-');
Note: See TracChangeset for help on using the changeset viewer.