note 90647 added to function.wordwrap

From: Date: Sat, 02 May 2009 11:07:34 +0000
Subject: note 90647 added to function.wordwrap
Groups: php.notes 
Request: Send a blank email to php-notes+get-154110@lists.php.net to get a copy of this message
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. ---- Server IP: 69.147.83.197 Probable Submitter: 81.88.120.206 ---- Manual Page -- http://www.php.net/manual/en/function.wordwrap.php Edit -- https://master.php.net/note/edit/90647 Del: integrated -- https://master.php.net/note/delete/90647/integrated Del: useless -- https://master.php.net/note/delete/90647/useless Del: bad code -- https://master.php.net/note/delete/90647/bad+code Del: spam -- https://master.php.net/note/delete/90647/spam Del: non-english -- https://master.php.net/note/delete/90647/non-english Del: in docs -- https://master.php.net/note/delete/90647/in+docs Del: other reasons-- https://master.php.net/note/delete/90647 Reject -- https://master.php.net/note/reject/90647 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#154110) next »