root/branches/2.0/wp-includes/functions-formatting.php

Revision 5932, 35.4 kB (checked in by westi, 1 year ago)

Enable plugins to override the cockney replace array, fixes #4804 for 2.0.12 props andihit

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 <?php
2
3 function wptexturize($text) {
4     global $wp_cockneyreplace;
5     $output = '';
6     // Capture tags and everything inside them
7     $textarr = preg_split("/(<.*>)/Us", $text, -1, PREG_SPLIT_DELIM_CAPTURE);
8     $stop = count($textarr); $next = true; // loop stuff
9     for ($i = 0; $i < $stop; $i++) {
10         $curl = $textarr[$i];
11
12         if (isset($curl{0}) && '<' != $curl{0} && $next) { // If it's not a tag
13             $curl = str_replace('---', '&#8212;', $curl);
14             $curl = str_replace(' -- ', ' &#8212; ', $curl);
15             $curl = str_replace('--', '&#8211;', $curl);
16             $curl = str_replace('xn&#8211;', 'xn--', $curl);
17             $curl = str_replace('...', '&#8230;', $curl);
18             $curl = str_replace('``', '&#8220;', $curl);
19
20             // if a plugin has provided an autocorrect array, use it
21             if ( isset($wp_cockneyreplace) ) {
22                 $cockney = array_keys($wp_cockneyreplace);
23                 $cockneyreplace = array_values($wp_cockneyreplace);
24             } else {
25                 $cockney = array("'tain't","'twere","'twas","'tis","'twill","'til","'bout","'nuff","'round","'cause");
26                 $cockneyreplace = array("&#8217;tain&#8217;t","&#8217;twere","&#8217;twas","&#8217;tis","&#8217;twill","&#8217;til","&#8217;bout","&#8217;nuff","&#8217;round","&#8217;cause");
27             }
28
29             $curl = str_replace($cockney, $cockneyreplace, $curl);
30
31             $curl = preg_replace("/'s/", '&#8217;s', $curl);
32             $curl = preg_replace("/'(\d\d(?:&#8217;|')?s)/", "&#8217;$1", $curl);
33             $curl = preg_replace('/(\s|\A|")\'/', '$1&#8216;', $curl);
34             $curl = preg_replace('/(\d+)"/', '$1&#8243;', $curl);
35             $curl = preg_replace("/(\d+)'/", '$1&#8242;', $curl);
36             $curl = preg_replace("/(\S)'([^'\s])/", "$1&#8217;$2", $curl);
37             $curl = preg_replace('/(\s|\A)"(?!\s)/', '$1&#8220;$2', $curl);
38             $curl = preg_replace('/"(\s|\S|\Z)/', '&#8221;$1', $curl);
39             $curl = preg_replace("/'([\s.]|\Z)/", '&#8217;$1', $curl);
40             $curl = preg_replace("/ \(tm\)/i", ' &#8482;', $curl);
41             $curl = str_replace("''", '&#8221;', $curl);
42             
43             $curl = preg_replace('/(\d+)x(\d+)/', "$1&#215;$2", $curl);
44
45         } elseif (strstr($curl, '<code') || strstr($curl, '<pre') || strstr($curl, '<kbd' || strstr($curl, '<style') || strstr($curl, '<script'))) {
46             // strstr is fast
47             $next = false;
48         } else {
49             $next = true;
50         }
51         $curl = preg_replace('/&([^#])(?![a-zA-Z1-4]{1,8};)/', '&#038;$1', $curl);
52         $output .= $curl;
53     }
54     return $output;
55 }
56
57 function clean_pre($text) {
58     $text = str_replace('<br />', '', $text);
59     $text = str_replace('<p>', "\n", $text);
60     $text = str_replace('</p>', '', $text);
61     return $text;
62 }
63
64 function wpautop($pee, $br = 1) {
65     $pee = $pee . "\n"; // just to make things a little easier, pad the end
66     $pee = preg_replace('|<br />\s*<br />|', "\n\n", $pee);
67     // Space things out a little
68     $pee = preg_replace('!(<(?:table|thead|tfoot|caption|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|select|form|blockquote|address|math|p|h[1-6])[^>]*>)!', "\n$1", $pee);
69     $pee = preg_replace('!(</(?:table|thead|tfoot|caption|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|select|form|blockquote|address|math|p|h[1-6])>)!', "$1\n\n", $pee);
70     $pee = str_replace(array("\r\n", "\r"), "\n", $pee); // cross-platform newlines
71     $pee = preg_replace("/\n\n+/", "\n\n", $pee); // take care of duplicates
72     $pee = preg_replace('/\n?(.+?)(?:\n\s*\n|\z)/s', "<p>$1</p>\n", $pee); // make paragraphs, including one at the end
73     $pee = preg_replace('|<p>\s*?</p>|', '', $pee); // under certain strange conditions it could create a P of entirely whitespace
74     $pee = preg_replace('!<p>\s*(</?(?:table|thead|tfoot|caption|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|hr|pre|select|form|blockquote|address|math|p|h[1-6])[^>]*>)\s*</p>!', "$1", $pee); // don't pee all over a tag
75     $pee = preg_replace("|<p>(<li.+?)</p>|", "$1", $pee); // problem with nested lists
76     $pee = preg_replace('|<p><blockquote([^>]*)>|i', "<blockquote$1><p>", $pee);
77     $pee = str_replace('</blockquote></p>', '</p></blockquote>', $pee);
78     $pee = preg_replace('!<p>\s*(</?(?:table|thead|tfoot|caption|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|hr|pre|select|form|blockquote|address|math|p|h[1-6])[^>]*>)!', "$1", $pee);
79     $pee = preg_replace('!(</?(?:table|thead|tfoot|caption|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|select|form|blockquote|address|math|p|h[1-6])[^>]*>)\s*</p>!', "$1", $pee);
80     if ($br) $pee = preg_replace('|(?<!<br />)\s*\n|', "<br />\n", $pee); // optionally make line breaks
81     $pee = preg_replace('!(</?(?:table|thead|tfoot|caption|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|select|form|blockquote|address|math|p|h[1-6])[^>]*>)\s*<br />!', "$1", $pee);
82     $pee = preg_replace('!<br />(\s*</?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)[^>]*>)!', '$1', $pee);
83     $pee = preg_replace('!(<pre.*?>)(.*?)</pre>!ise', " stripslashes('$1') .  stripslashes(clean_pre('$2'))  . '</pre>' ", $pee);
84     
85     return $pee;
86 }
87
88
89 function seems_utf8($Str) { # by bmorel at ssi dot fr
90     for ($i=0; $i<strlen($Str); $i++) {
91         if (ord($Str[$i]) < 0x80) continue; # 0bbbbbbb
92         elseif ((ord($Str[$i]) & 0xE0) == 0xC0) $n=1; # 110bbbbb
93         elseif ((ord($Str[$i]) & 0xF0) == 0xE0) $n=2; # 1110bbbb
94         elseif ((ord($Str[$i]) & 0xF8) == 0xF0) $n=3; # 11110bbb
95         elseif ((ord($Str[$i]) & 0xFC) == 0xF8) $n=4; # 111110bb
96         elseif ((ord($Str[$i]) & 0xFE) == 0xFC) $n=5; # 1111110b
97         else return false; # Does not match any model
98         for ($j=0; $j<$n; $j++) { # n bytes matching 10bbbbbb follow ?
99             if ((++$i == strlen($Str)) || ((ord($Str[$i]) & 0xC0) != 0x80))
100             return false;
101         }
102     }
103     return true;
104 }
105
106 function wp_specialchars( $text, $quotes = 0 ) {
107     // Like htmlspecialchars except don't double-encode HTML entities
108     $text = preg_replace('/&([^#])(?![a-z1-4]{1,8};)/', '&#038;$1', $text);
109     $text = str_replace('<', '&lt;', $text);
110     $text = str_replace('>', '&gt;', $text);
111     if ( 'double' === $quotes ) {
112         $text = str_replace('"', '&quot;', $text);
113     } elseif ( 'single' === $quotes ) {
114         $text = str_replace("'", '&#039;', $text);
115     } elseif ( $quotes ) {
116         $text = str_replace('"', '&quot;', $text);
117         $text = str_replace("'", '&#039;', $text);
118     }
119     return $text;
120 }
121
122 function utf8_uri_encode( $utf8_string ) {
123   $unicode = '';       
124   $values = array();
125   $num_octets = 1;
126         
127   for ($i = 0; $i < strlen( $utf8_string ); $i++ ) {
128
129     $value = ord( $utf8_string[ $i ] );
130             
131     if ( $value < 128 ) {
132       $unicode .= chr($value);
133     } else {
134       if ( count( $values ) == 0 ) $num_octets = ( $value < 224 ) ? 2 : 3;
135                 
136       $values[] = $value;
137       
138       if ( count( $values ) == $num_octets ) {
139     if ($num_octets == 3) {
140       $unicode .= '%' . dechex($values[0]) . '%' . dechex($values[1]) . '%' . dechex($values[2]);
141     } else {
142       $unicode .= '%' . dechex($values[0]) . '%' . dechex($values[1]);
143     }
144
145     $values = array();
146     $num_octets = 1;
147       }
148     }
149   }
150
151   return $unicode;   
152 }
153
154 function remove_accents($string) {
155     if ( !preg_match('/[\x80-\xff]/', $string) )
156         return $string;
157
158     if (seems_utf8($string)) {
159         $chars = array(
160         // Decompositions for Latin-1 Supplement
161         chr(195).chr(128) => 'A', chr(195).chr(129) => 'A',
162         chr(195).chr(130) => 'A', chr(195).chr(131) => 'A',
163         chr(195).chr(132) => 'A', chr(195).chr(133) => 'A',
164         chr(195).chr(135) => 'C', chr(195).chr(136) => 'E',
165         chr(195).chr(137) => 'E', chr(195).chr(138) => 'E',
166         chr(195).chr(139) => 'E', chr(195).chr(140) => 'I',
167         chr(195).chr(141) => 'I', chr(195).chr(142) => 'I',
168         chr(195).chr(143) => 'I', chr(195).chr(145) => 'N',
169         chr(195).chr(146) => 'O', chr(195).chr(147) => 'O',
170         chr(195).chr(148) => 'O', chr(195).chr(149) => 'O',
171         chr(195).chr(150) => 'O', chr(195).chr(153) => 'U',
172         chr(195).chr(154) => 'U', chr(195).chr(155) => 'U',
173         chr(195).chr(156) => 'U', chr(195).chr(157) => 'Y',
174         chr(195).chr(159) => 's', chr(195).chr(160) => 'a',
175         chr(195).chr(161) => 'a', chr(195).chr(162) => 'a',
176         chr(195).chr(163) => 'a', chr(195).chr(164) => 'a',
177         chr(195).chr(165) => 'a', chr(195).chr(167) => 'c',
178         chr(195).chr(168) => 'e', chr(195).chr(169) => 'e',
179         chr(195).chr(170) => 'e', chr(195).chr(171) => 'e',
180         chr(195).chr(172) => 'i', chr(195).chr(173) => 'i',
181         chr(195).chr(174) => 'i', chr(195).chr(175) => 'i',
182         chr(195).chr(177) => 'n', chr(195).chr(178) => 'o',
183         chr(195).chr(179) => 'o', chr(195).chr(180) => 'o',
184         chr(195).chr(181) => 'o', chr(195).chr(182) => 'o',
185         chr(195).chr(182) => 'o', chr(195).chr(185) => 'u',
186         chr(195).chr(186) => 'u', chr(195).chr(187) => 'u',
187         chr(195).chr(188) => 'u', chr(195).chr(189) => 'y',
188         chr(195).chr(191) => 'y',
189         // Decompositions for Latin Extended-A
190         chr(196).chr(128) => 'A', chr(196).chr(129) => 'a',
191         chr(196).chr(130) => 'A', chr(196).chr(131) => 'a',
192         chr(196).chr(132) => 'A', chr(196).chr(133) => 'a',
193         chr(196).chr(134) => 'C', chr(196).chr(135) => 'c',
194         chr(196).chr(136) => 'C', chr(196).chr(137) => 'c',
195         chr(196).chr(138) => 'C', chr(196).chr(139) => 'c',
196         chr(196).chr(140) => 'C', chr(196).chr(141) => 'c',
197         chr(196).chr(142) => 'D', chr(196).chr(143) => 'd',
198         chr(196).chr(144) => 'D', chr(196).chr(145) => 'd',
199         chr(196).chr(146) => 'E', chr(196).chr(147) => 'e',
200         chr(196).chr(148) => 'E', chr(196).chr(149) => 'e',
201         chr(196).chr(150) => 'E', chr(196).chr(151) => 'e',
202         chr(196).chr(152) => 'E', chr(196).chr(153) => 'e',
203         chr(196).chr(154) => 'E', chr(196).chr(155) => 'e',
204         chr(196).chr(156) => 'G', chr(196).chr(157) => 'g',
205         chr(196).chr(158) => 'G', chr(196).chr(159) => 'g',
206         chr(196).chr(160) => 'G', chr(196).chr(161) => 'g',
207         chr(196).chr(162) => 'G', chr(196).chr(163) => 'g',
208         chr(196).chr(164) => 'H', chr(196).chr(165) => 'h',
209         chr(196).chr(166) => 'H', chr(196).chr(167) => 'h',
210         chr(196).chr(168) => 'I', chr(196).chr(169) => 'i',
211         chr(196).chr(170) => 'I', chr(196).chr(171) => 'i',
212         chr(196).chr(172) => 'I', chr(196).chr(173) => 'i',
213         chr(196).chr(174) => 'I', chr(196).chr(175) => 'i',
214         chr(196).chr(176) => 'I', chr(196).chr(177) => 'i',
215         chr(196).chr(178) => 'IJ',chr(196).chr(179) => 'ij',
216         chr(196).chr(180) => 'J', chr(196).chr(181) => 'j',
217         chr(196).chr(182) => 'K', chr(196).chr(183) => 'k',
218         chr(196).chr(184) => 'k', chr(196).chr(185) => 'L',
219         chr(196).chr(186) => 'l', chr(196).chr(187) => 'L',
220         chr(196).chr(188) => 'l', chr(196).chr(189) => 'L',
221         chr(196).chr(190) => 'l', chr(196).chr(191) => 'L',
222         chr(197).chr(128) => 'l', chr(197).chr(129) => 'L',
223         chr(197).chr(130) => 'l', chr(197).chr(131) => 'N',
224         chr(197).chr(132) => 'n', chr(197).chr(133) => 'N',
225         chr(197).chr(134) => 'n', chr(197).chr(135) => 'N',
226         chr(197).chr(136) => 'n', chr(197).chr(137) => 'N',
227         chr(197).chr(138) => 'n', chr(197).chr(139) => 'N',
228         chr(197).chr(140) => 'O', chr(197).chr(141) => 'o',
229         chr(197).chr(142) => 'O', chr(197).chr(143) => 'o',
230         chr(197).chr(144) => 'O', chr(197).chr(145) => 'o',
231         chr(197).chr(146) => 'OE',chr(197).chr(147) => 'oe',
232         chr(197).chr(148) => 'R',chr(197).chr(149) => 'r',
233         chr(197).chr(150) => 'R',chr(197).chr(151) => 'r',
234         chr(197).chr(152) => 'R',chr(197).chr(153) => 'r',
235         chr(197).chr(154) => 'S',chr(197).chr(155) => 's',
236         chr(197).chr(156) => 'S',chr(197).chr(157) => 's',
237         chr(197).chr(158) => 'S',chr(197).chr(159) => 's',
238         chr(197).chr(160) => 'S', chr(197).chr(161) => 's',
239         chr(197).chr(162) => 'T', chr(197).chr(163) => 't',
240         chr(197).chr(164) => 'T', chr(197).chr(165) => 't',
241         chr(197).chr(166) => 'T', chr(197).chr(167) => 't',
242         chr(197).chr(168) => 'U', chr(197).chr(169) => 'u',
243         chr(197