note 82392 deleted from function.wordwrap by danbrown
| From: | danbrown@php.net | Date: | Thu, 02 Apr 2009 13:37:35 +0000 |
| Subject: | note 82392 deleted from function.wordwrap by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-152760@lists.php.net to get a copy of this message | ||
Note Submitter: lqvang_NoSpamHere_79 at gmail dot com
----
@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