blob: 734cec104baead4b89dfed982fc755d0e38fce45 [file] [log] [blame]
Derek Jones4b9c6292011-07-01 17:40:48 -05001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
7 * @package CodeIgniter
8 * @author ExpressionEngine Dev Team
Greg Aker0711dc82011-01-05 10:49:40 -06009 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
Derek Allard2067d1a2008-11-13 22:59:24 +000010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
19 * Typography Class
20 *
21 *
22 * @access private
23 * @category Helpers
24 * @author ExpressionEngine Dev Team
25 * @link http://codeigniter.com/user_guide/helpers/
26 */
27class CI_Typography {
28
29 // Block level elements that should not be wrapped inside <p> tags
30 var $block_elements = 'address|blockquote|div|dl|fieldset|form|h\d|hr|noscript|object|ol|p|pre|script|table|ul';
Barry Mienydd671972010-10-04 16:33:58 +020031
Derek Allard2067d1a2008-11-13 22:59:24 +000032 // Elements that should not have <p> and <br /> tags within them.
Robin Sowell8db2d5e2009-07-17 22:07:04 +000033 var $skip_elements = 'p|pre|ol|ul|dl|object|table|h\d';
Barry Mienydd671972010-10-04 16:33:58 +020034
Derek Allard2067d1a2008-11-13 22:59:24 +000035 // Tags we want the parser to completely ignore when splitting the string.
36 var $inline_elements = 'a|abbr|acronym|b|bdo|big|br|button|cite|code|del|dfn|em|i|img|ins|input|label|map|kbd|q|samp|select|small|span|strong|sub|sup|textarea|tt|var';
Barry Mienydd671972010-10-04 16:33:58 +020037
Derek Jonesd5738d92008-11-14 16:53:34 +000038 // array of block level elements that require inner content to be within another block level element
39 var $inner_block_required = array('blockquote');
Barry Mienydd671972010-10-04 16:33:58 +020040
Derek Jonesd5738d92008-11-14 16:53:34 +000041 // the last block element parsed
42 var $last_block_element = '';
Barry Mienydd671972010-10-04 16:33:58 +020043
Derek Allard2067d1a2008-11-13 22:59:24 +000044 // whether or not to protect quotes within { curly braces }
45 var $protect_braced_quotes = FALSE;
Barry Mienydd671972010-10-04 16:33:58 +020046
Derek Allard2067d1a2008-11-13 22:59:24 +000047 /**
Derek Allard2067d1a2008-11-13 22:59:24 +000048 * Auto Typography
49 *
50 * This function converts text, making it typographically correct:
Barry Mienydd671972010-10-04 16:33:58 +020051 * - Converts double spaces into paragraphs.
52 * - Converts single line breaks into <br /> tags
53 * - Converts single and double quotes into correctly facing curly quote entities.
54 * - Converts three dots into ellipsis.
55 * - Converts double dashes into em-dashes.
Derek Jones4b9c6292011-07-01 17:40:48 -050056 * - Converts two spaces into entities
Derek Allard2067d1a2008-11-13 22:59:24 +000057 *
58 * @access public
59 * @param string
60 * @param bool whether to reduce more then two consecutive newlines to two
61 * @return string
62 */
63 function auto_typography($str, $reduce_linebreaks = FALSE)
64 {
65 if ($str == '')
66 {
67 return '';
68 }
69
70 // Standardize Newlines to make matching easier
71 if (strpos($str, "\r") !== FALSE)
72 {
Barry Mienydd671972010-10-04 16:33:58 +020073 $str = str_replace(array("\r\n", "\r"), "\n", $str);
Derek Allard2067d1a2008-11-13 22:59:24 +000074 }
Barry Mienydd671972010-10-04 16:33:58 +020075
Derek Jones4b9c6292011-07-01 17:40:48 -050076 // Reduce line breaks. If there are more than two consecutive linebreaks
Derek Allard2067d1a2008-11-13 22:59:24 +000077 // we'll compress them down to a maximum of two since there's no benefit to more.
78 if ($reduce_linebreaks === TRUE)
79 {
80 $str = preg_replace("/\n\n+/", "\n\n", $str);
Barry Mienydd671972010-10-04 16:33:58 +020081 }
Derek Allard2067d1a2008-11-13 22:59:24 +000082
Derek Jonesa633ec22008-12-11 14:31:33 +000083 // HTML comment tags don't conform to patterns of normal tags, so pull them out separately, only if needed
84 $html_comments = array();
85 if (strpos($str, '<!--') !== FALSE)
86 {
87 if (preg_match_all("#(<!\-\-.*?\-\->)#s", $str, $matches))
88 {
89 for ($i = 0, $total = count($matches[0]); $i < $total; $i++)
90 {
91 $html_comments[] = $matches[0][$i];
92 $str = str_replace($matches[0][$i], '{@HC'.$i.'}', $str);
93 }
94 }
95 }
Barry Mienydd671972010-10-04 16:33:58 +020096
Derek Jones4b9c6292011-07-01 17:40:48 -050097 // match and yank <pre> tags if they exist. It's cheaper to do this separately since most content will
Derek Jones7deecfb2008-12-11 15:38:01 +000098 // not contain <pre> tags, and it keeps the PCRE patterns below simpler and faster
99 if (strpos($str, '<pre') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000100 {
Derek Jones7deecfb2008-12-11 15:38:01 +0000101 $str = preg_replace_callback("#<pre.*?>.*?</pre>#si", array($this, '_protect_characters'), $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000102 }
Barry Mienydd671972010-10-04 16:33:58 +0200103
Derek Jones7deecfb2008-12-11 15:38:01 +0000104 // Convert quotes within tags to temporary markers.
105 $str = preg_replace_callback("#<.+?>#si", array($this, '_protect_characters'), $str);
106
107 // Do the same with braces if necessary
Derek Allard2067d1a2008-11-13 22:59:24 +0000108 if ($this->protect_braced_quotes === TRUE)
109 {
Barry Mienydd671972010-10-04 16:33:58 +0200110 $str = preg_replace_callback("#\{.+?\}#si", array($this, '_protect_characters'), $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000111 }
Barry Mienydd671972010-10-04 16:33:58 +0200112
Derek Jones4b9c6292011-07-01 17:40:48 -0500113 // Convert "ignore" tags to temporary marker. The parser splits out the string at every tag
114 // it encounters. Certain inline tags, like image tags, links, span tags, etc. will be
Derek Allard2067d1a2008-11-13 22:59:24 +0000115 // adversely affected if they are split out so we'll convert the opening bracket < temporarily to: {@TAG}
116 $str = preg_replace("#<(/*)(".$this->inline_elements.")([ >])#i", "{@TAG}\\1\\2\\3", $str);
117
Derek Jones4b9c6292011-07-01 17:40:48 -0500118 // Split the string at every tag. This expression creates an array with this prototype:
Barry Mienydd671972010-10-04 16:33:58 +0200119 //
120 // [array]
121 // {
122 // [0] = <opening tag>
123 // [1] = Content...
124 // [2] = <closing tag>
125 // Etc...
126 // }
Derek Allard2067d1a2008-11-13 22:59:24 +0000127 $chunks = preg_split('/(<(?:[^<>]+(?:"[^"]*"|\'[^\']*\')?)+>)/', $str, -1, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY);
Barry Mienydd671972010-10-04 16:33:58 +0200128
Derek Jones4b9c6292011-07-01 17:40:48 -0500129 // Build our finalized string. We cycle through the array, skipping tags, and processing the contained text
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 $str = '';
131 $process = TRUE;
132 $paragraph = FALSE;
Derek Jonese4702122009-01-14 21:57:32 +0000133 $current_chunk = 0;
134 $total_chunks = count($chunks);
Barry Mienydd671972010-10-04 16:33:58 +0200135
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 foreach ($chunks as $chunk)
Barry Mienydd671972010-10-04 16:33:58 +0200137 {
Derek Jonese4702122009-01-14 21:57:32 +0000138 $current_chunk++;
Barry Mienydd671972010-10-04 16:33:58 +0200139
Derek Allard2067d1a2008-11-13 22:59:24 +0000140 // Are we dealing with a tag? If so, we'll skip the processing for this cycle.
141 // Well also set the "process" flag which allows us to skip <pre> tags and a few other things.
Derek Jones7deecfb2008-12-11 15:38:01 +0000142 if (preg_match("#<(/*)(".$this->block_elements.").*?>#", $chunk, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000143 {
144 if (preg_match("#".$this->skip_elements."#", $match[2]))
145 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500146 $process = ($match[1] == '/') ? TRUE : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000147 }
Barry Mienydd671972010-10-04 16:33:58 +0200148
Derek Jonesd5738d92008-11-14 16:53:34 +0000149 if ($match[1] == '')
150 {
151 $this->last_block_element = $match[2];
152 }
153
Derek Allard2067d1a2008-11-13 22:59:24 +0000154 $str .= $chunk;
155 continue;
156 }
Barry Mienydd671972010-10-04 16:33:58 +0200157
Derek Allard2067d1a2008-11-13 22:59:24 +0000158 if ($process == FALSE)
159 {
Derek Jones7deecfb2008-12-11 15:38:01 +0000160 $str .= $chunk;
Derek Allard2067d1a2008-11-13 22:59:24 +0000161 continue;
162 }
Barry Mienydd671972010-10-04 16:33:58 +0200163
Derek Jones4b9c6292011-07-01 17:40:48 -0500164 // Force a newline to make sure end tags get processed by _format_newlines()
Derek Jonese4702122009-01-14 21:57:32 +0000165 if ($current_chunk == $total_chunks)
166 {
Barry Mienydd671972010-10-04 16:33:58 +0200167 $chunk .= "\n";
Derek Jonese4702122009-01-14 21:57:32 +0000168 }
Barry Mienydd671972010-10-04 16:33:58 +0200169
Derek Jones4b9c6292011-07-01 17:40:48 -0500170 // Convert Newlines into <p> and <br /> tags
Derek Jones7deecfb2008-12-11 15:38:01 +0000171 $str .= $this->_format_newlines($chunk);
Derek Allard2067d1a2008-11-13 22:59:24 +0000172 }
Barry Mienydd671972010-10-04 16:33:58 +0200173
Derek Jones4b9c6292011-07-01 17:40:48 -0500174 // No opening block level tag? Add it if needed.
Derek Jonese4702122009-01-14 21:57:32 +0000175 if ( ! preg_match("/^\s*<(?:".$this->block_elements.")/i", $str))
Derek Allard2067d1a2008-11-13 22:59:24 +0000176 {
Derek Jonese4702122009-01-14 21:57:32 +0000177 $str = preg_replace("/^(.*?)<(".$this->block_elements.")/i", '<p>$1</p><$2', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000178 }
Barry Mienydd671972010-10-04 16:33:58 +0200179
Derek Jones7deecfb2008-12-11 15:38:01 +0000180 // Convert quotes, elipsis, em-dashes, non-breaking spaces, and ampersands
181 $str = $this->format_characters($str);
Barry Mienydd671972010-10-04 16:33:58 +0200182
Derek Jonesa633ec22008-12-11 14:31:33 +0000183 // restore HTML comments
184 for ($i = 0, $total = count($html_comments); $i < $total; $i++)
185 {
Derek Jones4777fb82008-12-11 17:55:42 +0000186 // remove surrounding paragraph tags, but only if there's an opening paragraph tag
187 // otherwise HTML comments at the ends of paragraphs will have the closing tag removed
188 // if '<p>{@HC1}' then replace <p>{@HC1}</p> with the comment, else replace only {@HC1} with the comment
189 $str = preg_replace('#(?(?=<p>\{@HC'.$i.'\})<p>\{@HC'.$i.'\}(\s*</p>)|\{@HC'.$i.'\})#s', $html_comments[$i], $str);
Derek Jonesa633ec22008-12-11 14:31:33 +0000190 }
Barry Mienydd671972010-10-04 16:33:58 +0200191
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 // Final clean up
193 $table = array(
Barry Mienydd671972010-10-04 16:33:58 +0200194
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 // If the user submitted their own paragraph tags within the text
196 // we will retain them instead of using our tags.
Derek Jonesc2067542010-10-01 07:19:57 -0500197 '/(<p[^>*?]>)<p>/' => '$1', // <?php BBEdit syntax coloring bug fix
Barry Mienydd671972010-10-04 16:33:58 +0200198
Derek Allard2067d1a2008-11-13 22:59:24 +0000199 // Reduce multiple instances of opening/closing paragraph tags to a single one
200 '#(</p>)+#' => '</p>',
201 '/(<p>\W*<p>)+/' => '<p>',
Barry Mienydd671972010-10-04 16:33:58 +0200202
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 // Clean up stray paragraph tags that appear before block level elements
204 '#<p></p><('.$this->block_elements.')#' => '<$1',
Derek Jonese4702122009-01-14 21:57:32 +0000205
Derek Allard2067d1a2008-11-13 22:59:24 +0000206 // Clean up stray non-breaking spaces preceeding block elements
Derek Jones4b9c6292011-07-01 17:40:48 -0500207 '#(&nbsp;\s*)+<('.$this->block_elements.')#' => ' <$2',
Derek Jonesd5738d92008-11-14 16:53:34 +0000208
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 // Replace the temporary markers we added earlier
210 '/\{@TAG\}/' => '<',
211 '/\{@DQ\}/' => '"',
212 '/\{@SQ\}/' => "'",
213 '/\{@DD\}/' => '--',
Derek Jones4b9c6292011-07-01 17:40:48 -0500214 '/\{@NBS\}/' => ' ',
Derek Allard2067d1a2008-11-13 22:59:24 +0000215
Derek Jonesc2067542010-10-01 07:19:57 -0500216 // An unintended consequence of the _format_newlines function is that
217 // some of the newlines get truncated, resulting in <p> tags
Barry Mienydd671972010-10-04 16:33:58 +0200218 // starting immediately after <block> tags on the same line.
Derek Jonesc2067542010-10-01 07:19:57 -0500219 // This forces a newline after such occurrences, which looks much nicer.
220 "/><p>\n/" => ">\n<p>",
Barry Mienydd671972010-10-04 16:33:58 +0200221
Derek Jonesc2067542010-10-01 07:19:57 -0500222 // Similarly, there might be cases where a closing </block> will follow
223 // a closing </p> tag, so we'll correct it by adding a newline in between
224 "#</p></#" => "</p>\n</"
Derek Allard2067d1a2008-11-13 22:59:24 +0000225 );
Barry Mienydd671972010-10-04 16:33:58 +0200226
Derek Allard2067d1a2008-11-13 22:59:24 +0000227 // Do we need to reduce empty lines?
228 if ($reduce_linebreaks === TRUE)
229 {
230 $table['#<p>\n*</p>#'] = '';
231 }
232 else
233 {
234 // If we have empty paragraph tags we add a non-breaking space
235 // otherwise most browsers won't treat them as true paragraphs
236 $table['#<p></p>#'] = '<p>&nbsp;</p>';
237 }
Barry Mienydd671972010-10-04 16:33:58 +0200238
Derek Allard2067d1a2008-11-13 22:59:24 +0000239 return preg_replace(array_keys($table), $table, $str);
240
241 }
Barry Mienydd671972010-10-04 16:33:58 +0200242
Derek Allard2067d1a2008-11-13 22:59:24 +0000243 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200244
Derek Allard2067d1a2008-11-13 22:59:24 +0000245 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 * Format Characters
247 *
248 * This function mainly converts double and single quotes
249 * to curly entities, but it also converts em-dashes,
250 * double spaces, and ampersands
251 *
252 * @access public
253 * @param string
254 * @return string
255 */
256 function format_characters($str)
257 {
258 static $table;
Barry Mienydd671972010-10-04 16:33:58 +0200259
Derek Allard2067d1a2008-11-13 22:59:24 +0000260 if ( ! isset($table))
261 {
Barry Mienydd671972010-10-04 16:33:58 +0200262 $table = array(
Derek Allard2067d1a2008-11-13 22:59:24 +0000263 // nested smart quotes, opening and closing
264 // note that rules for grammar (English) allow only for two levels deep
265 // and that single quotes are _supposed_ to always be on the outside
266 // but we'll accommodate both
Derek Jonesb859df82008-11-18 15:24:20 +0000267 // Note that in all cases, whitespace is the primary determining factor
268 // on which direction to curl, with non-word characters like punctuation
269 // being a secondary factor only after whitespace is addressed.
270 '/\'"(\s|$)/' => '&#8217;&#8221;$1',
Derek Jones232484c2009-01-15 19:10:48 +0000271 '/(^|\s|<p>)\'"/' => '$1&#8216;&#8220;',
Derek Jonesb859df82008-11-18 15:24:20 +0000272 '/\'"(\W)/' => '&#8217;&#8221;$1',
273 '/(\W)\'"/' => '$1&#8216;&#8220;',
274 '/"\'(\s|$)/' => '&#8221;&#8217;$1',
Derek Jones232484c2009-01-15 19:10:48 +0000275 '/(^|\s|<p>)"\'/' => '$1&#8220;&#8216;',
Derek Jonesb859df82008-11-18 15:24:20 +0000276 '/"\'(\W)/' => '&#8221;&#8217;$1',
277 '/(\W)"\'/' => '$1&#8220;&#8216;',
Derek Allard2067d1a2008-11-13 22:59:24 +0000278
279 // single quote smart quotes
Derek Jonesb859df82008-11-18 15:24:20 +0000280 '/\'(\s|$)/' => '&#8217;$1',
Derek Jones232484c2009-01-15 19:10:48 +0000281 '/(^|\s|<p>)\'/' => '$1&#8216;',
Derek Jonesb859df82008-11-18 15:24:20 +0000282 '/\'(\W)/' => '&#8217;$1',
283 '/(\W)\'/' => '$1&#8216;',
Derek Allard2067d1a2008-11-13 22:59:24 +0000284
285 // double quote smart quotes
Derek Jonesb859df82008-11-18 15:24:20 +0000286 '/"(\s|$)/' => '&#8221;$1',
Derek Jones232484c2009-01-15 19:10:48 +0000287 '/(^|\s|<p>)"/' => '$1&#8220;',
Derek Jonesb859df82008-11-18 15:24:20 +0000288 '/"(\W)/' => '&#8221;$1',
289 '/(\W)"/' => '$1&#8220;',
290
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 // apostrophes
Derek Jonesb859df82008-11-18 15:24:20 +0000292 "/(\w)'(\w)/" => '$1&#8217;$2',
Derek Allard2067d1a2008-11-13 22:59:24 +0000293
294 // Em dash and ellipses dots
295 '/\s?\-\-\s?/' => '&#8212;',
296 '/(\w)\.{3}/' => '$1&#8230;',
297
298 // double space after sentences
Derek Jones4b9c6292011-07-01 17:40:48 -0500299 '/(\W) /' => '$1&nbsp; ',
Derek Allard2067d1a2008-11-13 22:59:24 +0000300
301 // ampersands, if not a character entity
302 '/&(?!#?[a-zA-Z0-9]{2,};)/' => '&amp;'
Derek Jonesb859df82008-11-18 15:24:20 +0000303 );
304 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000305
306 return preg_replace(array_keys($table), $table, $str);
307 }
Barry Mienydd671972010-10-04 16:33:58 +0200308
Derek Allard2067d1a2008-11-13 22:59:24 +0000309 // --------------------------------------------------------------------
310
311 /**
312 * Format Newlines
313 *
314 * Converts newline characters into either <p> tags or <br />
315 *
316 * @access public
317 * @param string
318 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200319 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 function _format_newlines($str)
321 {
322 if ($str == '')
323 {
324 return $str;
325 }
Barry Mienydd671972010-10-04 16:33:58 +0200326
Derek Jones4b9c6292011-07-01 17:40:48 -0500327 if (strpos($str, "\n") === FALSE && ! in_array($this->last_block_element, $this->inner_block_required))
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 {
329 return $str;
330 }
Barry Mienydd671972010-10-04 16:33:58 +0200331
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 // Convert two consecutive newlines to paragraphs
333 $str = str_replace("\n\n", "</p>\n\n<p>", $str);
Barry Mienydd671972010-10-04 16:33:58 +0200334
Derek Allard2067d1a2008-11-13 22:59:24 +0000335 // Convert single spaces to <br /> tags
336 $str = preg_replace("/([^\n])(\n)([^\n])/", "\\1<br />\\2\\3", $str);
Barry Mienydd671972010-10-04 16:33:58 +0200337
Derek Allard2067d1a2008-11-13 22:59:24 +0000338 // Wrap the whole enchilada in enclosing paragraphs
339 if ($str != "\n")
340 {
Derek Jonesc2067542010-10-01 07:19:57 -0500341 // We trim off the right-side new line so that the closing </p> tag
342 // will be positioned immediately following the string, matching
343 // the behavior of the opening <p> tag
Derek Jones4b9c6292011-07-01 17:40:48 -0500344 $str = '<p>'.rtrim($str).'</p>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 }
346
347 // Remove empty paragraphs if they are on the first line, as this
348 // is a potential unintended consequence of the previous code
349 $str = preg_replace("/<p><\/p>(.*)/", "\\1", $str, 1);
Barry Mienydd671972010-10-04 16:33:58 +0200350
Derek Allard2067d1a2008-11-13 22:59:24 +0000351 return $str;
352 }
Barry Mienydd671972010-10-04 16:33:58 +0200353
Derek Allard2067d1a2008-11-13 22:59:24 +0000354 // ------------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200355
Derek Allard2067d1a2008-11-13 22:59:24 +0000356 /**
Derek Jones7deecfb2008-12-11 15:38:01 +0000357 * Protect Characters
358 *
359 * Protects special characters from being formatted later
360 * We don't want quotes converted within tags so we'll temporarily convert them to {@DQ} and {@SQ}
Barry Mienydd671972010-10-04 16:33:58 +0200361 * and we don't want double dashes converted to emdash entities, so they are marked with {@DD}
362 * likewise double spaces are converted to {@NBS} to prevent entity conversion
Derek Jones7deecfb2008-12-11 15:38:01 +0000363 *
364 * @access public
365 * @param array
366 * @return string
367 */
368 function _protect_characters($match)
369 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500370 return str_replace(array("'",'"','--',' '), array('{@SQ}', '{@DQ}', '{@DD}', '{@NBS}'), $match[0]);
Derek Jones7deecfb2008-12-11 15:38:01 +0000371 }
372
373 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200374
Derek Jones7deecfb2008-12-11 15:38:01 +0000375 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 * Convert newlines to HTML line breaks except within PRE tags
377 *
378 * @access public
379 * @param string
380 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200381 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000382 function nl2br_except_pre($str)
383 {
384 $ex = explode("pre>",$str);
385 $ct = count($ex);
Barry Mienydd671972010-10-04 16:33:58 +0200386
Derek Allard2067d1a2008-11-13 22:59:24 +0000387 $newstr = "";
388 for ($i = 0; $i < $ct; $i++)
389 {
390 if (($i % 2) == 0)
391 {
392 $newstr .= nl2br($ex[$i]);
393 }
394 else
395 {
396 $newstr .= $ex[$i];
397 }
Barry Mienydd671972010-10-04 16:33:58 +0200398
Derek Allard2067d1a2008-11-13 22:59:24 +0000399 if ($ct - 1 != $i)
400 $newstr .= "pre>";
401 }
Barry Mienydd671972010-10-04 16:33:58 +0200402
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 return $newstr;
404 }
Barry Mienydd671972010-10-04 16:33:58 +0200405
Derek Allard2067d1a2008-11-13 22:59:24 +0000406}
407// END Typography Class
408
409/* End of file Typography.php */
Rick Ellis4c938ae2008-09-10 22:58:38 +0000410/* Location: ./system/libraries/Typography.php */