converted to unix-style eol
[www-register-wizard.git] / helpers / text_helper.php
index 8a44608..6e61f77 100644 (file)
-<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');\r
-/**\r
- * CodeIgniter\r
- *\r
- * An open source application development framework for PHP 4.3.2 or newer\r
- *\r
- * @package            CodeIgniter\r
- * @author             ExpressionEngine Dev Team\r
- * @copyright  Copyright (c) 2008, EllisLab, Inc.\r
- * @license            http://codeigniter.com/user_guide/license.html\r
- * @link               http://codeigniter.com\r
- * @since              Version 1.0\r
- * @filesource\r
- */\r
-\r
-// ------------------------------------------------------------------------\r
-\r
-/**\r
- * CodeIgniter Text Helpers\r
- *\r
- * @package            CodeIgniter\r
- * @subpackage Helpers\r
- * @category   Helpers\r
- * @author             ExpressionEngine Dev Team\r
- * @link               http://codeigniter.com/user_guide/helpers/text_helper.html\r
- */\r
-\r
-// ------------------------------------------------------------------------\r
-\r
-/**\r
- * Word Limiter\r
- *\r
- * Limits a string to X number of words.\r
- *\r
- * @access     public\r
- * @param      string\r
- * @param      integer\r
- * @param      string  the end character. Usually an ellipsis\r
- * @return     string\r
- */    \r
-if ( ! function_exists('word_limiter'))\r
-{\r
-       function word_limiter($str, $limit = 100, $end_char = '&#8230;')\r
-       {\r
-               if (trim($str) == '')\r
-               {\r
-                       return $str;\r
-               }\r
-       \r
-               preg_match('/^\s*+(?:\S++\s*+){1,'.(int) $limit.'}/', $str, $matches);\r
-                       \r
-               if (strlen($str) == strlen($matches[0]))\r
-               {\r
-                       $end_char = '';\r
-               }\r
-               \r
-               return rtrim($matches[0]).$end_char;\r
-       }\r
-}\r
-       \r
-// ------------------------------------------------------------------------\r
-\r
-/**\r
- * Character Limiter\r
- *\r
- * Limits the string based on the character count.  Preserves complete words\r
- * so the character count may not be exactly as specified.\r
- *\r
- * @access     public\r
- * @param      string\r
- * @param      integer\r
- * @param      string  the end character. Usually an ellipsis\r
- * @return     string\r
- */    \r
-if ( ! function_exists('character_limiter'))\r
-{\r
-       function character_limiter($str, $n = 500, $end_char = '&#8230;')\r
-       {\r
-               if (strlen($str) < $n)\r
-               {\r
-                       return $str;\r
-               }\r
-               \r
-               $str = preg_replace("/\s+/", ' ', str_replace(array("\r\n", "\r", "\n"), ' ', $str));\r
-\r
-               if (strlen($str) <= $n)\r
-               {\r
-                       return $str;\r
-               }\r
-                                                                       \r
-               $out = "";\r
-               foreach (explode(' ', trim($str)) as $val)\r
-               {\r
-                       $out .= $val.' ';                       \r
-                       if (strlen($out) >= $n)\r
-                       {\r
-                               return trim($out).$end_char;\r
-                       }               \r
-               }\r
-       }\r
-}\r
-       \r
-// ------------------------------------------------------------------------\r
-\r
-/**\r
- * High ASCII to Entities\r
- *\r
- * Converts High ascii text and MS Word special characters to character entities\r
- *\r
- * @access     public\r
- * @param      string\r
- * @return     string\r
- */    \r
-if ( ! function_exists('ascii_to_entities'))\r
-{\r
-       function ascii_to_entities($str)\r
-       {\r
-          $count       = 1;\r
-          $out = '';\r
-          $temp        = array();\r
-       \r
-          for ($i = 0, $s = strlen($str); $i < $s; $i++)\r
-          {\r
-                  $ordinal = ord($str[$i]);\r
-       \r
-                  if ($ordinal < 128)\r
-                  {\r
-                          $out .= $str[$i];\r
-                  }\r
-                  else\r
-                  {\r
-                          if (count($temp) == 0)\r
-                          {\r
-                                  $count = ($ordinal < 224) ? 2 : 3;\r
-                          }\r
-               \r
-                          $temp[] = $ordinal;\r
-               \r
-                          if (count($temp) == $count)\r
-                          {\r
-                                  $number = ($count == 3) ? (($temp['0'] % 16) * 4096) + (($temp['1'] % 64) * 64) + ($temp['2'] % 64) : (($temp['0'] % 32) * 64) + ($temp['1'] % 64);\r
-\r
-                                  $out .= '&#'.$number.';';\r
-                                  $count = 1;\r
-                                  $temp = array();\r
-                          }\r
-                  }\r
-          }\r
-\r
-          return $out;\r
-       }\r
-}\r
-       \r
-// ------------------------------------------------------------------------\r
-\r
-/**\r
- * Entities to ASCII\r
- *\r
- * Converts character entities back to ASCII\r
- *\r
- * @access     public\r
- * @param      string\r
- * @param      bool\r
- * @return     string\r
- */    \r
-if ( ! function_exists('entities_to_ascii'))\r
-{\r
-       function entities_to_ascii($str, $all = TRUE)\r
-       {\r
-          if (preg_match_all('/\&#(\d+)\;/', $str, $matches))\r
-          {\r
-                  for ($i = 0, $s = count($matches['0']); $i < $s; $i++)\r
-                  {                            \r
-                          $digits = $matches['1'][$i];\r
-\r
-                          $out = '';\r
-\r
-                          if ($digits < 128)\r
-                          {\r
-                                  $out .= chr($digits);\r
-               \r
-                          }\r
-                          elseif ($digits < 2048)\r
-                          {\r
-                                  $out .= chr(192 + (($digits - ($digits % 64)) / 64));\r
-                                  $out .= chr(128 + ($digits % 64));\r
-                          }\r
-                          else\r
-                          {\r
-                                  $out .= chr(224 + (($digits - ($digits % 4096)) / 4096));\r
-                                  $out .= chr(128 + ((($digits % 4096) - ($digits % 64)) / 64));\r
-                                  $out .= chr(128 + ($digits % 64));\r
-                          }\r
-\r
-                          $str = str_replace($matches['0'][$i], $out, $str);                           \r
-                  }\r
-          }\r
-\r
-          if ($all)\r
-          {\r
-                  $str = str_replace(array("&amp;", "&lt;", "&gt;", "&quot;", "&apos;", "&#45;"),\r
-                                                         array("&","<",">","\"", "'", "-"),\r
-                                                         $str);\r
-          }\r
-\r
-          return $str;\r
-       }\r
-}\r
-       \r
-// ------------------------------------------------------------------------\r
-\r
-/**\r
- * Word Censoring Function\r
- *\r
- * Supply a string and an array of disallowed words and any\r
- * matched words will be converted to #### or to the replacement\r
- * word you've submitted.\r
- *\r
- * @access     public\r
- * @param      string  the text string\r
- * @param      string  the array of censoered words\r
- * @param      string  the optional replacement value\r
- * @return     string\r
- */    \r
-if ( ! function_exists('word_censor'))\r
-{\r
-       function word_censor($str, $censored, $replacement = '')\r
-       {\r
-               if ( ! is_array($censored))\r
-               {\r
-                       return $str;\r
-               }\r
-\r
-               $str = ' '.$str.' ';\r
-               foreach ($censored as $badword)\r
-               {\r
-                       if ($replacement != '')\r
-                       {\r
-                               $str = preg_replace("/\b(".str_replace('\*', '\w*?', preg_quote($badword, '/')).")\b/i", $replacement, $str);\r
-                       }\r
-                       else\r
-                       {\r
-                               $str = preg_replace("/\b(".str_replace('\*', '\w*?', preg_quote($badword, '/')).")\b/ie", "str_repeat('#', strlen('\\1'))", $str);\r
-                       }\r
-               }\r
-       \r
-               return trim($str);\r
-       }\r
-}\r
-       \r
-// ------------------------------------------------------------------------\r
-\r
-/**\r
- * Code Highlighter\r
- *\r
- * Colorizes code strings\r
- *\r
- * @access     public\r
- * @param      string  the text string\r
- * @return     string\r
- */    \r
-if ( ! function_exists('highlight_code'))\r
-{\r
-       function highlight_code($str)\r
-       {               \r
-               // The highlight string function encodes and highlights\r
-               // brackets so we need them to start raw\r
-               $str = str_replace(array('&lt;', '&gt;'), array('<', '>'), $str);\r
-       \r
-               // Replace any existing PHP tags to temporary markers so they don't accidentally\r
-               // break the string out of PHP, and thus, thwart the highlighting.\r
-       \r
-               $str = str_replace(array('<?', '?>', '<%', '%>', '\\', '</script>'), \r
-                                                       array('phptagopen', 'phptagclose', 'asptagopen', 'asptagclose', 'backslashtmp', 'scriptclose'), $str);\r
-\r
-               // The highlight_string function requires that the text be surrounded\r
-               // by PHP tags, which we will remove later\r
-               $str = '<?php '.$str.' ?>'; // <?\r
-\r
-               // All the magic happens here, baby!    \r
-               $str = highlight_string($str, TRUE);\r
-\r
-               // Prior to PHP 5, the highligh function used icky <font> tags\r
-               // so we'll replace them with <span> tags.\r
-\r
-               if (abs(PHP_VERSION) < 5)\r
-               {\r
-                       $str = str_replace(array('<font ', '</font>'), array('<span ', '</span>'), $str);\r
-                       $str = preg_replace('#color="(.*?)"#', 'style="color: \\1"', $str);\r
-               }\r
-               \r
-               // Remove our artificially added PHP, and the syntax highlighting that came with it\r
-               $str = preg_replace('/<span style="color: #([A-Z0-9]+)">&lt;\?php(&nbsp;| )/i', '<span style="color: #$1">', $str);\r
-               $str = preg_replace('/(<span style="color: #[A-Z0-9]+">.*?)\?&gt;<\/span>\n<\/span>\n<\/code>/is', "$1</span>\n</span>\n</code>", $str);\r
-               $str = preg_replace('/<span style="color: #[A-Z0-9]+"\><\/span>/i', '', $str);\r
-                       \r
-               // Replace our markers back to PHP tags.\r
-               $str = str_replace(array('phptagopen', 'phptagclose', 'asptagopen', 'asptagclose', 'backslashtmp', 'scriptclose'),\r
-                                                       array('&lt;?', '?&gt;', '&lt;%', '%&gt;', '\\', '&lt;/script&gt;'), $str);\r
-                                                                               \r
-               return $str;\r
-       }\r
-}\r
-       \r
-// ------------------------------------------------------------------------\r
-\r
-/**\r
- * Phrase Highlighter\r
- *\r
- * Highlights a phrase within a text string\r
- *\r
- * @access     public\r
- * @param      string  the text string\r
- * @param      string  the phrase you'd like to highlight\r
- * @param      string  the openging tag to precede the phrase with\r
- * @param      string  the closing tag to end the phrase with\r
- * @return     string\r
- */    \r
-if ( ! function_exists('highlight_phrase'))\r
-{\r
-       function highlight_phrase($str, $phrase, $tag_open = '<strong>', $tag_close = '</strong>')\r
-       {\r
-               if ($str == '')\r
-               {\r
-                       return '';\r
-               }\r
-       \r
-               if ($phrase != '')\r
-               {\r
-                       return preg_replace('/('.preg_quote($phrase, '/').')/i', $tag_open."\\1".$tag_close, $str);\r
-               }\r
-\r
-               return $str;\r
-       }\r
-}\r
-       \r
-// ------------------------------------------------------------------------\r
-\r
-/**\r
- * Word Wrap\r
- *\r
- * Wraps text at the specified character.  Maintains the integrity of words.\r
- * Anything placed between {unwrap}{/unwrap} will not be word wrapped, nor\r
- * will URLs.\r
- *\r
- * @access     public\r
- * @param      string  the text string\r
- * @param      integer the number of characters to wrap at\r
- * @return     string\r
- */    \r
-if ( ! function_exists('word_wrap'))\r
-{\r
-       function word_wrap($str, $charlim = '76')\r
-       {\r
-               // Se the character limit\r
-               if ( ! is_numeric($charlim))\r
-                       $charlim = 76;\r
-       \r
-               // Reduce multiple spaces\r
-               $str = preg_replace("| +|", " ", $str);\r
-       \r
-               // Standardize newlines\r
-               if (strpos($str, "\r") !== FALSE)\r
-               {\r
-                       $str = str_replace(array("\r\n", "\r"), "\n", $str);                    \r
-               }\r
-       \r
-               // If the current word is surrounded by {unwrap} tags we'll \r
-               // strip the entire chunk and replace it with a marker.\r
-               $unwrap = array();\r
-               if (preg_match_all("|(\{unwrap\}.+?\{/unwrap\})|s", $str, $matches))\r
-               {\r
-                       for ($i = 0; $i < count($matches['0']); $i++)\r
-                       {\r
-                               $unwrap[] = $matches['1'][$i];                          \r
-                               $str = str_replace($matches['1'][$i], "{{unwrapped".$i."}}", $str);\r
-                       }\r
-               }\r
-       \r
-               // Use PHP's native function to do the initial wordwrap.  \r
-               // We set the cut flag to FALSE so that any individual words that are \r
-               // too long get left alone.  In the next step we'll deal with them.\r
-               $str = wordwrap($str, $charlim, "\n", FALSE);\r
-       \r
-               // Split the string into individual lines of text and cycle through them\r
-               $output = "";\r
-               foreach (explode("\n", $str) as $line) \r
-               {\r
-                       // Is the line within the allowed character count?\r
-                       // If so we'll join it to the output and continue\r
-                       if (strlen($line) <= $charlim)\r
-                       {\r
-                               $output .= $line."\n";                  \r
-                               continue;\r
-                       }\r
-                       \r
-                       $temp = '';\r
-                       while((strlen($line)) > $charlim) \r
-                       {\r
-                               // If the over-length word is a URL we won't wrap it\r
-                               if (preg_match("!\[url.+\]|://|wwww.!", $line))\r
-                               {\r
-                                       break;\r
-                               }\r
-\r
-                               // Trim the word down\r
-                               $temp .= substr($line, 0, $charlim-1);\r
-                               $line = substr($line, $charlim-1);\r
-                       }\r
-               \r
-                       // If $temp contains data it means we had to split up an over-length \r
-                       // word into smaller chunks so we'll add it back to our current line\r
-                       if ($temp != '')\r
-                       {\r
-                               $output .= $temp . "\n" . $line; \r
-                       }\r
-                       else\r
-                       {\r
-                               $output .= $line;\r
-                       }\r
-\r
-                       $output .= "\n";\r
-               }\r
-\r
-               // Put our markers back\r
-               if (count($unwrap) > 0)\r
-               {       \r
-                       foreach ($unwrap as $key => $val)\r
-                       {\r
-                               $output = str_replace("{{unwrapped".$key."}}", $val, $output);\r
-                       }\r
-               }\r
-\r
-               // Remove the unwrap tags\r
-               $output = str_replace(array('{unwrap}', '{/unwrap}'), '', $output);\r
-\r
-               return $output; \r
-       }\r
-}\r
-\r
-\r
-/* End of file text_helper.php */\r
+<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
+/**
+ * CodeIgniter
+ *
+ * An open source application development framework for PHP 4.3.2 or newer
+ *
+ * @package            CodeIgniter
+ * @author             ExpressionEngine Dev Team
+ * @copyright  Copyright (c) 2008, EllisLab, Inc.
+ * @license            http://codeigniter.com/user_guide/license.html
+ * @link               http://codeigniter.com
+ * @since              Version 1.0
+ * @filesource
+ */
+
+// ------------------------------------------------------------------------
+
+/**
+ * CodeIgniter Text Helpers
+ *
+ * @package            CodeIgniter
+ * @subpackage Helpers
+ * @category   Helpers
+ * @author             ExpressionEngine Dev Team
+ * @link               http://codeigniter.com/user_guide/helpers/text_helper.html
+ */
+
+// ------------------------------------------------------------------------
+
+/**
+ * Word Limiter
+ *
+ * Limits a string to X number of words.
+ *
+ * @access     public
+ * @param      string
+ * @param      integer
+ * @param      string  the end character. Usually an ellipsis
+ * @return     string
+ */    
+if ( ! function_exists('word_limiter'))
+{
+       function word_limiter($str, $limit = 100, $end_char = '&#8230;')
+       {
+               if (trim($str) == '')
+               {
+                       return $str;
+               }
+       
+               preg_match('/^\s*+(?:\S++\s*+){1,'.(int) $limit.'}/', $str, $matches);
+                       
+               if (strlen($str) == strlen($matches[0]))
+               {
+                       $end_char = '';
+               }
+               
+               return rtrim($matches[0]).$end_char;
+       }
+}
+       
+// ------------------------------------------------------------------------
+
+/**
+ * Character Limiter
+ *
+ * Limits the string based on the character count.  Preserves complete words
+ * so the character count may not be exactly as specified.
+ *
+ * @access     public
+ * @param      string
+ * @param      integer
+ * @param      string  the end character. Usually an ellipsis
+ * @return     string
+ */    
+if ( ! function_exists('character_limiter'))
+{
+       function character_limiter($str, $n = 500, $end_char = '&#8230;')
+       {
+               if (strlen($str) < $n)
+               {
+                       return $str;
+               }
+               
+               $str = preg_replace("/\s+/", ' ', str_replace(array("\r\n", "\r", "\n"), ' ', $str));
+
+               if (strlen($str) <= $n)
+               {
+                       return $str;
+               }
+                                                                       
+               $out = "";
+               foreach (explode(' ', trim($str)) as $val)
+               {
+                       $out .= $val.' ';                       
+                       if (strlen($out) >= $n)
+                       {
+                               return trim($out).$end_char;
+                       }               
+               }
+       }
+}
+       
+// ------------------------------------------------------------------------
+
+/**
+ * High ASCII to Entities
+ *
+ * Converts High ascii text and MS Word special characters to character entities
+ *
+ * @access     public
+ * @param      string
+ * @return     string
+ */    
+if ( ! function_exists('ascii_to_entities'))
+{
+       function ascii_to_entities($str)
+       {
+          $count       = 1;
+          $out = '';
+          $temp        = array();
+       
+          for ($i = 0, $s = strlen($str); $i < $s; $i++)
+          {
+                  $ordinal = ord($str[$i]);
+       
+                  if ($ordinal < 128)
+                  {
+                          $out .= $str[$i];
+                  }
+                  else
+                  {
+                          if (count($temp) == 0)
+                          {
+                                  $count = ($ordinal < 224) ? 2 : 3;
+                          }
+               
+                          $temp[] = $ordinal;
+               
+                          if (count($temp) == $count)
+                          {
+                                  $number = ($count == 3) ? (($temp['0'] % 16) * 4096) + (($temp['1'] % 64) * 64) + ($temp['2'] % 64) : (($temp['0'] % 32) * 64) + ($temp['1'] % 64);
+
+                                  $out .= '&#'.$number.';';
+                                  $count = 1;
+                                  $temp = array();
+                          }
+                  }
+          }
+
+          return $out;
+       }
+}
+       
+// ------------------------------------------------------------------------
+
+/**
+ * Entities to ASCII
+ *
+ * Converts character entities back to ASCII
+ *
+ * @access     public
+ * @param      string
+ * @param      bool
+ * @return     string
+ */    
+if ( ! function_exists('entities_to_ascii'))
+{
+       function entities_to_ascii($str, $all = TRUE)
+       {
+          if (preg_match_all('/\&#(\d+)\;/', $str, $matches))
+          {
+                  for ($i = 0, $s = count($matches['0']); $i < $s; $i++)
+                  {                            
+                          $digits = $matches['1'][$i];
+
+                          $out = '';
+
+                          if ($digits < 128)
+                          {
+                                  $out .= chr($digits);
+               
+                          }
+                          elseif ($digits < 2048)
+                          {
+                                  $out .= chr(192 + (($digits - ($digits % 64)) / 64));
+                                  $out .= chr(128 + ($digits % 64));
+                          }
+                          else
+                          {
+                                  $out .= chr(224 + (($digits - ($digits % 4096)) / 4096));
+                                  $out .= chr(128 + ((($digits % 4096) - ($digits % 64)) / 64));
+                                  $out .= chr(128 + ($digits % 64));
+                          }
+
+                          $str = str_replace($matches['0'][$i], $out, $str);                           
+                  }
+          }
+
+          if ($all)
+          {
+                  $str = str_replace(array("&amp;", "&lt;", "&gt;", "&quot;", "&apos;", "&#45;"),
+                                                         array("&","<",">","\"", "'", "-"),
+                                                         $str);
+          }
+
+          return $str;
+       }
+}
+       
+// ------------------------------------------------------------------------
+
+/**
+ * Word Censoring Function
+ *
+ * Supply a string and an array of disallowed words and any
+ * matched words will be converted to #### or to the replacement
+ * word you've submitted.
+ *
+ * @access     public
+ * @param      string  the text string
+ * @param      string  the array of censoered words
+ * @param      string  the optional replacement value
+ * @return     string
+ */    
+if ( ! function_exists('word_censor'))
+{
+       function word_censor($str, $censored, $replacement = '')
+       {
+               if ( ! is_array($censored))
+               {
+                       return $str;
+               }
+
+               $str = ' '.$str.' ';
+               foreach ($censored as $badword)
+               {
+                       if ($replacement != '')
+                       {
+                               $str = preg_replace("/\b(".str_replace('\*', '\w*?', preg_quote($badword, '/')).")\b/i", $replacement, $str);
+                       }
+                       else
+                       {
+                               $str = preg_replace("/\b(".str_replace('\*', '\w*?', preg_quote($badword, '/')).")\b/ie", "str_repeat('#', strlen('\\1'))", $str);
+                       }
+               }
+       
+               return trim($str);
+       }
+}
+       
+// ------------------------------------------------------------------------
+
+/**
+ * Code Highlighter
+ *
+ * Colorizes code strings
+ *
+ * @access     public
+ * @param      string  the text string
+ * @return     string
+ */    
+if ( ! function_exists('highlight_code'))
+{
+       function highlight_code($str)
+       {               
+               // The highlight string function encodes and highlights
+               // brackets so we need them to start raw
+               $str = str_replace(array('&lt;', '&gt;'), array('<', '>'), $str);
+       
+               // Replace any existing PHP tags to temporary markers so they don't accidentally
+               // break the string out of PHP, and thus, thwart the highlighting.
+       
+               $str = str_replace(array('<?', '?>', '<%', '%>', '\\', '</script>'), 
+                                                       array('phptagopen', 'phptagclose', 'asptagopen', 'asptagclose', 'backslashtmp', 'scriptclose'), $str);
+
+               // The highlight_string function requires that the text be surrounded
+               // by PHP tags, which we will remove later
+               $str = '<?php '.$str.' ?>'; // <?
+
+               // All the magic happens here, baby!    
+               $str = highlight_string($str, TRUE);
+
+               // Prior to PHP 5, the highligh function used icky <font> tags
+               // so we'll replace them with <span> tags.
+
+               if (abs(PHP_VERSION) < 5)
+               {
+                       $str = str_replace(array('<font ', '</font>'), array('<span ', '</span>'), $str);
+                       $str = preg_replace('#color="(.*?)"#', 'style="color: \\1"', $str);
+               }
+               
+               // Remove our artificially added PHP, and the syntax highlighting that came with it
+               $str = preg_replace('/<span style="color: #([A-Z0-9]+)">&lt;\?php(&nbsp;| )/i', '<span style="color: #$1">', $str);
+               $str = preg_replace('/(<span style="color: #[A-Z0-9]+">.*?)\?&gt;<\/span>\n<\/span>\n<\/code>/is', "$1</span>\n</span>\n</code>", $str);
+               $str = preg_replace('/<span style="color: #[A-Z0-9]+"\><\/span>/i', '', $str);
+                       
+               // Replace our markers back to PHP tags.
+               $str = str_replace(array('phptagopen', 'phptagclose', 'asptagopen', 'asptagclose', 'backslashtmp', 'scriptclose'),
+                                                       array('&lt;?', '?&gt;', '&lt;%', '%&gt;', '\\', '&lt;/script&gt;'), $str);
+                                                                               
+               return $str;
+       }
+}
+       
+// ------------------------------------------------------------------------
+
+/**
+ * Phrase Highlighter
+ *
+ * Highlights a phrase within a text string
+ *
+ * @access     public
+ * @param      string  the text string
+ * @param      string  the phrase you'd like to highlight
+ * @param      string  the openging tag to precede the phrase with
+ * @param      string  the closing tag to end the phrase with
+ * @return     string
+ */    
+if ( ! function_exists('highlight_phrase'))
+{
+       function highlight_phrase($str, $phrase, $tag_open = '<strong>', $tag_close = '</strong>')
+       {
+               if ($str == '')
+               {
+                       return '';
+               }
+       
+               if ($phrase != '')
+               {
+                       return preg_replace('/('.preg_quote($phrase, '/').')/i', $tag_open."\\1".$tag_close, $str);
+               }
+
+               return $str;
+       }
+}
+       
+// ------------------------------------------------------------------------
+
+/**
+ * Word Wrap
+ *
+ * Wraps text at the specified character.  Maintains the integrity of words.
+ * Anything placed between {unwrap}{/unwrap} will not be word wrapped, nor
+ * will URLs.
+ *
+ * @access     public
+ * @param      string  the text string
+ * @param      integer the number of characters to wrap at
+ * @return     string
+ */    
+if ( ! function_exists('word_wrap'))
+{
+       function word_wrap($str, $charlim = '76')
+       {
+               // Se the character limit
+               if ( ! is_numeric($charlim))
+                       $charlim = 76;
+       
+               // Reduce multiple spaces
+               $str = preg_replace("| +|", " ", $str);
+       
+               // Standardize newlines
+               if (strpos($str, "\r") !== FALSE)
+               {
+                       $str = str_replace(array("\r\n", "\r"), "\n", $str);                    
+               }
+       
+               // If the current word is surrounded by {unwrap} tags we'll 
+               // strip the entire chunk and replace it with a marker.
+               $unwrap = array();
+               if (preg_match_all("|(\{unwrap\}.+?\{/unwrap\})|s", $str, $matches))
+               {
+                       for ($i = 0; $i < count($matches['0']); $i++)
+                       {
+                               $unwrap[] = $matches['1'][$i];                          
+                               $str = str_replace($matches['1'][$i], "{{unwrapped".$i."}}", $str);
+                       }
+               }
+       
+               // Use PHP's native function to do the initial wordwrap.  
+               // We set the cut flag to FALSE so that any individual words that are 
+               // too long get left alone.  In the next step we'll deal with them.
+               $str = wordwrap($str, $charlim, "\n", FALSE);
+       
+               // Split the string into individual lines of text and cycle through them
+               $output = "";
+               foreach (explode("\n", $str) as $line) 
+               {
+                       // Is the line within the allowed character count?
+                       // If so we'll join it to the output and continue
+                       if (strlen($line) <= $charlim)
+                       {
+                               $output .= $line."\n";                  
+                               continue;
+                       }
+                       
+                       $temp = '';
+                       while((strlen($line)) > $charlim) 
+                       {
+                               // If the over-length word is a URL we won't wrap it
+                               if (preg_match("!\[url.+\]|://|wwww.!", $line))
+                               {
+                                       break;
+                               }
+
+                               // Trim the word down
+                               $temp .= substr($line, 0, $charlim-1);
+                               $line = substr($line, $charlim-1);
+                       }
+               
+                       // If $temp contains data it means we had to split up an over-length 
+                       // word into smaller chunks so we'll add it back to our current line
+                       if ($temp != '')
+                       {
+                               $output .= $temp . "\n" . $line; 
+                       }
+                       else
+                       {
+                               $output .= $line;
+                       }
+
+                       $output .= "\n";
+               }
+
+               // Put our markers back
+               if (count($unwrap) > 0)
+               {       
+                       foreach ($unwrap as $key => $val)
+                       {
+                               $output = str_replace("{{unwrapped".$key."}}", $val, $output);
+                       }
+               }
+
+               // Remove the unwrap tags
+               $output = str_replace(array('{unwrap}', '{/unwrap}'), '', $output);
+
+               return $output; 
+       }
+}
+
+
+/* End of file text_helper.php */
 /* Location: ./system/helpers/text_helper.php */
\ No newline at end of file