note 90647 deleted from function.wordwrap by danbrown
| From: | danbrown@php.net | Date: | Sun, 17 May 2009 18:26:09 +0000 |
| Subject: | note 90647 deleted from function.wordwrap by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-155113@lists.php.net to get a copy of this message | ||
Note Submitter: dunk at quake dot ru
----
I've had troubles with long words entered into textarea screwing my site design, so I decided
to write a word wrapping script which will only wrap WORDS, but will not touch length of sentances
since if theres a white space (" ") between words they're line breaking automatically
when needed.
<?php
// Word-wrapping function by Nikitin Aleksey;
// Enter string which may contain long words you want to wrap,
// Max length of the word which will not wrapped,
// and how you want to break it "- " or " " or "<br>" are
examples for html.
function wrapper($str, $max, $break){
$len=strlen($str); // getting length of the string.
if($len>$max){ // if length < our max word lenght then do nothing.
$limited = str_split($str,$max); // spliting our string into array
$maxrows=$len/$max; // number of elements in array
$i=0; // while counter
while($i<$maxrows){ // check all elements of array one by one
$numofspaces = substr_count($limited[$i]," ");// checking if theres whitespace in element
of array
if($numofspaces<1){$limited[$i] .= "$break";}// if theres no whitespace adding our
$break to this element.
$i=$i+1; // counter
}
return implode("",$limited);
}
return $str;
};
?>
Function returns text with added "break" to the end of last allowed symbol of long word
and then writeing rest part of the word.