note 82392 added to function.wordwrap

From: Date: Wed, 09 Apr 2008 09:05:19 +0000
Subject: note 82392 added to function.wordwrap
Groups: php.notes 
Request: Send a blank email to php-notes+get-138047@lists.php.net to get a copy of this message
@znupiDONTWANTSPAM69 at gmail dot comBLAH the layout_wrap function doesn't work as I expected. Following is my version. It may consume more resource but gives solid result: <?php function longword_break($str, $i) { if ((empty($str))||(strlen($str)<$i)){ return $str; } $words_arr = explode(' ', $str); foreach ($words_arr as &$word){ $br_word = explode("\n", $word); //Process some run-in words conected by "\n" foreach ($br_word as &$r_word){ $j=$i; while($j < strlen($r_word)) { $r_word = substr($r_word, 0, $j) . ' '. substr($r_word, $j); $j+= ($i +1); } } $word =implode("\n", $br_word); unset($r_word); } unset($word); $str = implode(' ',$words_arr); //return nl2br(wordwrap($str, $i,"\n", true)); //return nl2br($str); return $str; } // Some test: $str = "This is 0123456789 aaaaaa ttttttttttttttttttttt ccc gggggggggg"; $str1 = "aaaaaaaaaaatessssssssssssssssstxxx"; echo "Orgininal string:<br />" . $str . "<br />"; echo "Layout wrap function: <br />" . layout_wrap($str,5). "<br />"; echo "Long word break function: <br/>" . longword_break($str,5). "<br />"; ?> Output: Orgininal string: This is 0123456789 aaaaaa ttttttttttttttttttttt ccc gggggggggg Layout wrap function: This is 0123456 789 aaaaaa ttttttt tttt tttt tttt tt ccc ggggggg ggg <!--Notice some "words" are still longer than 5 characters. /--> Long word break function: This is 01234 56789 aaaaa a ttttt ttttt ttttt ttttt t ccc ggggg ggggg ---- Server IP: 211.43.210.124 Probable Submitter: 117.16.153.71 ---- Manual Page -- http://www.php.net/manual/en/function.wordwrap.php Edit -- https://master.php.net/note/edit/82392 Del: integrated -- https://master.php.net/note/delete/82392/integrated Del: useless -- https://master.php.net/note/delete/82392/useless Del: bad code -- https://master.php.net/note/delete/82392/bad+code Del: spam -- https://master.php.net/note/delete/82392/spam Del: non-english -- https://master.php.net/note/delete/82392/non-english Del: in docs -- https://master.php.net/note/delete/82392/in+docs Del: other reasons-- https://master.php.net/note/delete/82392 Reject -- https://master.php.net/note/reject/82392 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#138047) next »