summaryrefslogtreecommitdiffstats
path: root/system/helpers/text_helper.php
diff options
context:
space:
mode:
authoradmin <devnull@localhost>2006-10-28 04:50:53 +0200
committeradmin <devnull@localhost>2006-10-28 04:50:53 +0200
commit86c60398852a299aa4a485c7e9e3c1d45e8c2184 (patch)
tree8b8becf9d1bb314ae29f8cbd36c68a235bdfe5f9 /system/helpers/text_helper.php
parent6895a4b6045dd3fef3a2e742932721f6ccc2a569 (diff)
Diffstat (limited to 'system/helpers/text_helper.php')
-rw-r--r--system/helpers/text_helper.php105
1 files changed, 70 insertions, 35 deletions
diff --git a/system/helpers/text_helper.php b/system/helpers/text_helper.php
index fbc63a9b4..1b9178129 100644
--- a/system/helpers/text_helper.php
+++ b/system/helpers/text_helper.php
@@ -325,62 +325,97 @@ function highlight_phrase($str, $phrase, $tag_open = '<strong>', $tag_close = '<
* 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
*/
-function word_wrap($str, $chars = '76')
-{
- if ( ! is_numeric($chars))
- $chars = 76;
+function word_wrap($str, $charlim = '76')
+{
+ // Se the character limit
+ if ( ! is_numeric($charlim))
+ $charlim = 76;
+
+ // Reduce multiple spaces
+ $str = preg_replace("| +|", " ", $str);
- $str = preg_replace("/(\r\n|\r|\n)/", "\n", $str);
- $lines = split("\n", $str);
+ // Standardize newlines
+ $str = preg_replace("/\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 = "";
- while (list(, $thisline) = each($lines))
+ foreach (explode("\n", $str) as $line)
{
- if (strlen($thisline) > $chars)
+ // 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.$this->newline;
+ continue;
+ }
+
+ $temp = '';
+ while((strlen($line)) > $charlim)
{
- $line = "";
- $words = split(" ", $thisline);
- while(list(, $thisword) = each($words))
+ // If the over-length word is a URL we won't wrap it
+ if (preg_match("!\[url.+\]|://|wwww.!", $line))
{
- while((strlen($thisword)) > $chars)
- {
- $cur_pos = 0;
- for($i=0; $i < $chars - 1; $i++)
- {
- $output .= $thisword[$i];
- $cur_pos++;
- }
-
- $output .= "\n";
- $thisword = substr($thisword, $cur_pos, (strlen($thisword) - $cur_pos));
- }
-
- if ((strlen($line) + strlen($thisword)) > $chars)
- {
- $output .= $line."\n";
- $line = $thisword." ";
- }
- else
- {
- $line .= $thisword." ";
- }
+ break;
}
- $output .= $line."\n";
+ // 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.$this->newline.$line;
}
else
{
- $output .= $thisline."\n";
+ $output .= $line;
+ }
+
+ $output .= $this->newline;
+ }
+
+ // 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;
}
+
?> \ No newline at end of file