Make WordPress Core

source: trunk/wp-includes/functions-formatting.php @ 955

Last change on this file since 955 was 955, checked in by saxmatt, 21 years ago

Migrating more of the global options to get_settings().

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.4 KB
Line 
1<?php
2
3function wptexturize($text) {
4        $output = "";
5        $textarr = preg_split("/(<.*>)/Us", $text, -1, PREG_SPLIT_DELIM_CAPTURE); // capture the tags as well as in between
6        $stop = count($textarr); $next = true; // loop stuff
7        for ($i = 0; $i < $stop; $i++) {
8                $curl = $textarr[$i];
9
10                if (isset($curl{0}) && '<' != $curl{0} && $next) { // If it's not a tag
11                        $curl = str_replace('---', '&#8212;', $curl);
12                        $curl = str_replace('--', '&#8211;', $curl);
13                        $curl = str_replace("...", '&#8230;', $curl);
14                        $curl = str_replace('``', '&#8220;', $curl);
15
16                        // This is a hack, look at this more later. It works pretty well though.
17                        $cockney = array("'tain't","'twere","'twas","'tis","'twill","'til","'bout","'nuff","'round");
18                        $cockneyreplace = array("&#8217;tain&#8217;t","&#8217;twere","&#8217;twas","&#8217;tis","&#8217;twill","&#8217;til","&#8217;bout","&#8217;nuff","&#8217;round");
19                        $curl = str_replace($cockney, $cockneyreplace, $curl);
20
21                        $curl = preg_replace("/'s/", "&#8217;s", $curl);
22                        $curl = preg_replace("/'(\d\d(?:&#8217;|')?s)/", "&#8217;$1", $curl);
23                        $curl = preg_replace('/(\s|\A|")\'/', '$1&#8216;', $curl);
24                        $curl = preg_replace("/(\d+)\"/", "$1&Prime;", $curl);
25                        $curl = preg_replace("/(\d+)'/", "$1&prime;", $curl);
26                        $curl = preg_replace("/(\S)'([^'\s])/", "$1&#8217;$2", $curl);
27                        $curl = preg_replace('/"([\s.,!?;:&\']|\Z)/', '&#8221;$1', $curl);
28            $curl = preg_replace('/(\s|\A)"/', '$1&#8220;', $curl);
29                        $curl = preg_replace("/'([\s.]|\Z)/", '&#8217;$1', $curl);
30                        $curl = preg_replace("/\(tm\)/i", '&#8482;', $curl);
31                        $curl = preg_replace("/\(c\)/i", '&#169;', $curl);
32                        $curl = preg_replace("/\(r\)/i", '&#174;', $curl);
33                        $curl = preg_replace('/&([^#])(?![a-z]{1,8};)/', '&#038;$1', $curl);
34                        $curl = str_replace("''", '&#8221;', $curl);
35                       
36                        $curl = preg_replace('/(d+)x(\d+)/', "$1&#215;$2", $curl);
37
38                } elseif (strstr($curl, '<code') || strstr($curl, '<pre') || strstr($curl, '<kbd' || strstr($curl, '<style') || strstr($curl, '<script'))) {
39                        // strstr is fast
40                        $next = false;
41                } else {
42                        $next = true;
43                }
44                $output .= $curl;
45        }
46        return $output;
47}
48
49function wpautop($pee, $br = 1) {
50        $pee = $pee . "\n"; // just to make things a little easier, pad the end
51        $pee = preg_replace('|<br />\s*<br />|', "\n\n", $pee);
52        $pee = preg_replace('!(<(?:table|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|select|form|blockquote|p|h[1-6])[^>]*>)!', "\n$1", $pee); // Space things out a little
53        $pee = preg_replace('!(</(?:table|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|select|form|blockquote|p|h[1-6])>)!', "$1\n", $pee); // Space things out a little
54        $pee = preg_replace("/(\r\n|\r)/", "\n", $pee); // cross-platform newlines
55        $pee = preg_replace("/\n\n+/", "\n\n", $pee); // take care of duplicates
56        $pee = preg_replace('/\n?(.+?)(?:\n\s*\n|\z)/s', "\t<p>$1</p>\n", $pee); // make paragraphs, including one at the end
57        $pee = preg_replace('|<p>\s*?</p>|', '', $pee); // under certain strange conditions it could create a P of entirely whitespace
58    $pee = preg_replace('!<p>\s*(</?(?:table|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|select|form|blockquote|p|h[1-6])[^>]*>)\s*</p>!', "$1", $pee); // don't pee all over a tag
59        $pee = preg_replace("|<p>(<li.+?)</p>|", "$1", $pee); // problem with nested lists
60        $pee = preg_replace('|<p><blockquote([^>]*)>|i', "<blockquote$1><p>", $pee);
61        $pee = str_replace('</blockquote></p>', '</p></blockquote>', $pee);
62        $pee = preg_replace('!<p>\s*(</?(?:table|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|select|form|blockquote|p|h[1-6])[^>]*>)!', "$1", $pee);
63        $pee = preg_replace('!(</?(?:table|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|select|form|blockquote|p|h[1-6])[^>]*>)\s*</p>!', "$1", $pee); 
64        if ($br) $pee = preg_replace('|(?<!<br />)\s*\n|', "<br />\n", $pee); // optionally make line breaks
65        $pee = preg_replace('!(</?(?:table|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|select|form|blockquote|p|h[1-6])[^>]*>)\s*<br />!', "$1", $pee);
66        $pee = preg_replace('!<br />(\s*</?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)>)!', '$1', $pee);
67        $pee = preg_replace('/&([^#])(?![a-z]{1,8};)/', '&#038;$1', $pee);
68       
69        return $pee; 
70}
71
72function sanitize_title($title) {
73    $title = strtolower($title);
74        $title = preg_replace('/&.+?;/', '', $title); // kill entities
75    $title = preg_replace('/[^a-z0-9 -]/', '', $title);
76    $title = preg_replace('/\s+/', ' ', $title);
77    $title = trim($title);
78    $title = str_replace(' ', '-', $title);
79        $title = preg_replace('|-+|', '-', $title);
80        return $title;
81}
82
83function convert_chars($content, $flag='obsolete attribute left there for backwards compatibility') { // html/unicode entities output
84        global $use_htmltrans, $wp_htmltrans, $wp_htmltranswinuni;
85
86        // removes metadata tags
87        $content = preg_replace('/<title>(.+?)<\/title>/','',$content);
88        $content = preg_replace('/<category>(.+?)<\/category>/','',$content);
89       
90        if ($use_htmltrans) {
91                // converts lone & characters into &#38; (a.k.a. &amp;)
92                $content = preg_replace('/&[^#](?![a-z]*;)/ie', '"&#38;".substr("\0",1)', $content);
93                // converts HTML-entities to their display values in order to convert them again later
94                $content = preg_replace('/['.chr(127).'-'.chr(255).']/e', '"&#".ord(\'\0\').";"', $content );
95                $content = strtr($content, $wp_htmltrans);
96                // now converting: Windows CP1252 => Unicode (valid HTML)
97                // (if you've ever pasted text from MSWord, you'll understand)
98                $content = strtr($content, $wp_htmltranswinuni);
99        }
100
101        // you can delete these 2 lines if you don't like <br /> and <hr />
102        $content = str_replace("<br>","<br />",$content);
103        $content = str_replace("<hr>","<hr />",$content);
104
105        return $content;
106}
107
108/*
109 balanceTags
110 
111 Balances Tags of string using a modified stack.
112 
113 @param text      Text to be balanced
114 @return          Returns balanced text
115 @author          Leonard Lin ([email protected])
116 @version         v1.1
117 @date            November 4, 2001
118 @license         GPL v2.0
119 @notes           
120 @changelog       
121             1.2  ***TODO*** Make better - change loop condition to $text
122             1.1  Fixed handling of append/stack pop order of end text
123                  Added Cleaning Hooks
124             1.0  First Version
125*/
126function balanceTags($text, $is_comment = 0) {
127        global $use_balanceTags;
128       
129        if ($use_balanceTags == 0) {
130                return $text;
131        }
132
133        $tagstack = array(); $stacksize = 0; $tagqueue = ''; $newtext = '';
134
135        # WP bug fix for comments - in case you REALLY meant to type '< !--'
136        $text = str_replace('< !--', '<    !--', $text);
137        # WP bug fix for LOVE <3 (and other situations with '<' before a number)
138        $text = preg_replace('#<([0-9]{1})#', '&lt;$1', $text);
139
140        while (preg_match("/<(\/?\w*)\s*([^>]*)>/",$text,$regex)) {
141                $newtext = $newtext . $tagqueue;
142
143                $i = strpos($text,$regex[0]);
144                $l = strlen($tagqueue) + strlen($regex[0]);
145
146                // clear the shifter
147                $tagqueue = '';
148                // Pop or Push
149                if ($regex[1][0] == "/") { // End Tag
150                        $tag = strtolower(substr($regex[1],1));
151                        // if too many closing tags
152                        if($stacksize <= 0) { 
153                                $tag = '';
154                                //or close to be safe $tag = '/' . $tag;
155                        }
156                        // if stacktop value = tag close value then pop
157                        else if ($tagstack[$stacksize - 1] == $tag) { // found closing tag
158                                $tag = '</' . $tag . '>'; // Close Tag
159                                // Pop
160                                array_pop ($tagstack);
161                                $stacksize--;
162                        } else { // closing tag not at top, search for it
163                                for ($j=$stacksize-1;$j>=0;$j--) {
164                                        if ($tagstack[$j] == $tag) {
165                                        // add tag to tagqueue
166                                                for ($k=$stacksize-1;$k>=$j;$k--){
167                                                        $tagqueue .= '</' . array_pop ($tagstack) . '>';
168                                                        $stacksize--;
169                                                }
170                                                break;
171                                        }
172                                }
173                                $tag = '';
174                        }
175                } else { // Begin Tag
176                        $tag = strtolower($regex[1]);
177
178                        // Tag Cleaning
179
180                        // Push if not img or br or hr
181                        if($tag != 'br' && $tag != 'img' && $tag != 'hr') {
182                                $stacksize = array_push ($tagstack, $tag);
183                        }
184
185                        // Attributes
186                        // $attributes = $regex[2];
187                        $attributes = $regex[2];
188                        if($attributes) {
189                                $attributes = ' '.$attributes;
190                        }
191                        $tag = '<'.$tag.$attributes.'>';
192                }
193                $newtext .= substr($text,0,$i) . $tag;
194                $text = substr($text,$i+$l);
195        } 
196
197        // Clear Tag Queue
198        $newtext = $newtext . $tagqueue;
199
200        // Add Remaining text
201        $newtext .= $text;
202
203        // Empty Stack
204        while($x = array_pop($tagstack)) {
205                $newtext = $newtext . '</' . $x . '>'; // Add remaining tags to close     
206        }
207
208        # WP fix for the bug with HTML comments
209        $newtext = str_replace("< !--","<!--",$newtext);
210        $newtext = str_replace("<    !--","< !--",$newtext);
211
212        return $newtext;
213}
214
215function autobrize($content) {
216        $content = preg_replace("/<br>\n/", "\n", $content);
217        $content = preg_replace("/<br \/>\n/", "\n", $content);
218        $content = preg_replace("/(\015\012)|(\015)|(\012)/", "<br />\n", $content);
219        return $content;
220}
221function unautobrize($content) {
222        $content = preg_replace("/<br>\n/", "\n", $content);   //for PHP versions before 4.0.5
223        $content = preg_replace("/<br \/>\n/", "\n", $content);
224        return $content;
225}
226
227
228function format_to_edit($content) {
229        global $autobr;
230        $content = stripslashes($content);
231        if ($autobr) { $content = unautobrize($content); }
232        $content = htmlspecialchars($content);
233        return $content;
234}
235function format_to_post($content) {
236        global $post_autobr,$comment_autobr;
237        $content = addslashes($content);
238        if ($post_autobr || $comment_autobr) { $content = autobrize($content); }
239        return $content;
240}
241
242function zeroise($number,$threshold) { // function to add leading zeros when necessary
243        $l=strlen($number);
244        if ($l<$threshold)
245                for ($i=0; $i<($threshold-$l); $i=$i+1) { $number='0'.$number;  }
246        return $number;
247        }
248
249
250function backslashit($string) {
251        $string = preg_replace('/([a-z])/i', '\\\\\1', $string);
252        return $string;
253}
254
255function trailingslashit($string) {
256    if ( '/' != substr($string, -1)) {
257        $string .= '/';
258    }
259    return $string;
260}
261
262function addslashes_gpc($gpc) {
263        if (!get_magic_quotes_gpc()) {
264                $gpc = addslashes($gpc);
265        }
266        return $gpc;
267}
268
269function antispambot($emailaddy, $mailto=0) {
270        $emailNOSPAMaddy = '';
271        srand ((float) microtime() * 1000000);
272        for ($i = 0; $i < strlen($emailaddy); $i = $i + 1) {
273                $j = floor(rand(0, 1+$mailto));
274                if ($j==0) {
275                        $emailNOSPAMaddy .= '&#'.ord(substr($emailaddy,$i,1)).';';
276                } elseif ($j==1) {
277                        $emailNOSPAMaddy .= substr($emailaddy,$i,1);
278                } elseif ($j==2) {
279                        $emailNOSPAMaddy .= '%'.zeroise(dechex(ord(substr($emailaddy, $i, 1))), 2);
280                }
281        }
282        $emailNOSPAMaddy = str_replace('@','&#64;',$emailNOSPAMaddy);
283        return $emailNOSPAMaddy;
284}
285
286function make_clickable($text) { // original function: phpBB, extended here for AIM & ICQ
287    $ret = " " . $text;
288    $ret = preg_replace("#([\n ])([a-z]+?)://([^, <>{}\n\r]+)#i", "\\1<a href=\"\\2://\\3\" target=\"_blank\">\\2://\\3</a>", $ret);
289    $ret = preg_replace("#([\n ])aim:([^,< \n\r]+)#i", "\\1<a href=\"aim:goim?screenname=\\2\\3&message=Hello\">\\2\\3</a>", $ret);
290    $ret = preg_replace("#([\n ])icq:([^,< \n\r]+)#i", "\\1<a href=\"http://wwp.icq.com/scripts/search.dll?to=\\2\\3\">\\2\\3</a>", $ret);
291    $ret = preg_replace("#([\n ])www\.([a-z0-9\-]+)\.([a-z0-9\-.\~]+)((?:/[^,< \n\r]*)?)#i", "\\1<a href=\"http://www.\\2.\\3\\4\" target=\"_blank\">www.\\2.\\3\\4</a>", $ret);
292    $ret = preg_replace("#([\n ])([a-z0-9\-_.]+?)@([^,< \n\r]+)#i", "\\1<a href=\"mailto:\\2@\\3\">\\2@\\3</a>", $ret);
293    $ret = substr($ret, 1);
294    return $ret;
295}
296
297function convert_bbcode($content) { // depreciated
298        return $content;
299}
300
301function convert_bbcode_email($content) {
302        global $use_bbcode;
303        $bbcode_email["in"] = array(
304                '#\[email](.+?)\[/email]#eis',
305                '#\[email=(.+?)](.+?)\[/email]#eis'
306        );
307        $bbcode_email["out"] = array(
308                "'<a href=\"mailto:'.antispambot('\\1').'\">'.antispambot('\\1').'</a>'",               // E-mail
309                "'<a href=\"mailto:'.antispambot('\\1').'\">\\2</a>'"
310        );
311
312        $content = preg_replace($bbcode_email["in"], $bbcode_email["out"], $content);
313        return $content;
314}
315
316function convert_gmcode($content) { // depreciated
317        return $content;
318}
319
320function convert_smilies($text) {
321        global $smilies_directory, $use_smilies;
322        global $wp_smiliessearch, $wp_smiliesreplace;
323    $output = '';
324        if ($use_smilies) {
325                // HTML loop taken from texturize function, could possible be consolidated
326                $textarr = preg_split("/(<.*>)/U", $text, -1, PREG_SPLIT_DELIM_CAPTURE); // capture the tags as well as in between
327                $stop = count($textarr);// loop stuff
328                for ($i = 0; $i < $stop; $i++) {
329                        $content = $textarr[$i];
330                        if ((strlen($content) > 0) && ('<' != $content{0})) { // If it's not a tag
331                                $content = str_replace($wp_smiliessearch, $wp_smiliesreplace, $content);
332                        }
333                        $output .= $content;
334                }
335        } else {
336                // return default text.
337                $output = $text;
338        }
339        return $output;
340}
341
342
343function is_email($user_email) {
344        $chars = "/^([a-z0-9_]|\\-|\\.)+@(([a-z0-9_]|\\-)+\\.)+[a-z]{2,4}\$/i";
345        if(strstr($user_email, '@') && strstr($user_email, '.')) {
346                if (preg_match($chars, $user_email)) {
347                        return true;
348                } else {
349                        return false;
350                }
351        } else {
352                return false;
353        }
354}
355
356
357function strip_all_but_one_link($text, $mylink) {
358        $match_link = '#(<a.+?href.+?'.'>)(.+?)(</a>)#';
359        preg_match_all($match_link, $text, $matches);
360        $count = count($matches[0]);
361        for ($i=0; $i<$count; $i++) {
362                if (!strstr($matches[0][$i], $mylink)) {
363                        $text = str_replace($matches[0][$i], $matches[2][$i], $text);
364                }
365        }
366        return $text;
367}
368
369
370// used by wp-mail to handle charsets in email subjects
371function wp_iso_descrambler($string) {
372  /* this may only work with iso-8859-1, I'm afraid */
373  if (!preg_match('#\=\?(.+)\?Q\?(.+)\?\=#i', $string, $matches)) {
374    return $string;
375  } else {
376    $subject = str_replace('_', ' ', $matches[2]);
377    $subject = preg_replace('#\=([0-9a-f]{2})#ei', "chr(hexdec(strtolower('$1')))", $subject);
378    return $subject;
379  }
380}
381
382
383// give it a date, it will give you the same date as GMT
384function get_gmt_from_date($string) {
385  // note: this only substracts $time_difference from the given date
386  $time_difference = get_settings('time_difference');
387  preg_match('#([0-9]{1,4})-([0-9]{1,2})-([0-9]{1,2}) ([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})#', $string, $matches);
388  $string_time = gmmktime($matches[4], $matches[5], $matches[6], $matches[2], $matches[3], $matches[1]);
389  $string_gmt = gmdate('Y-m-d H:i:s', $string_time - $time_difference*3600);
390  return $string_gmt;
391}
392
393// give it a GMT date, it will give you the same date with $time_difference added
394function get_date_from_gmt($string) {
395  // note: this only adds $time_difference to the given date
396  $time_difference = get_settings('time_difference');
397  preg_match('#([0-9]{1,4})-([0-9]{1,2})-([0-9]{1,2}) ([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})#', $string, $matches);
398  $string_time = gmmktime($matches[4], $matches[5], $matches[6], $matches[2], $matches[3], $matches[1]);
399  $string_localtime = gmdate('Y-m-d H:i:s', $string_time + $time_difference*3600);
400  return $string_localtime;
401}
402
403?>
Note: See TracBrowser for help on using the repository browser.