note 97380 modified in function.wordwrap by danbrown
| From: | danbrown@php.net | Date: | Fri, 16 Apr 2010 21:20:19 +0000 |
| Subject: | note 97380 modified in function.wordwrap by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-168329@lists.php.net to get a copy of this message | ||
I have a page with an image floating on the right, and text printed out beside the image (to the
left). I used wordwrap to wrap the text so that it doesn't go over the image, but sometimes the
text would go down beyond the image (vertically beyond the image, where the height of the text was
more than the height of the image), resulting in wrapped text that doesn't need to be wrapped.
So I came up with this method that takes a number max-lines to wordwrap, and then the rest either no
wrapping, or wordwrapping at a different number of characters:
<?php
// Wrap the text so it fits within the 35c width (to fit beside the picture)
$text = wordwrap($text, 35, "<br/>");
// The text comes below the picture, where we want wrapping to stop...
$maxLines = 18;
$newWidth = 80;
$lastPos = 1;
for ($i=0;$i<$maxLines;$i++)
{
$lastPos = strpos($text, '<br/>', $lastPos+1);
if ($lastPos === FALSE)
break;
}
$text = substr($text, 0, $lastPos) . "<br/>" .
wordwrap(str_replace('<br/>',' ',substr($text, $lastPos)), $newWidth,
'<br/>');
print $text;
?>
--was--
I have a page with an image floating on the right, and text printed out beside the image (to the
left). I used wordwrap to wrap the text so that it doesn't go over the image, but sometimes the
text would go down beyond the image (vertically beyond the image, where the height of the text was
more than the height of the image), resulting in wrapped text that doesn't need to be wrapped.
So I came up with this method that takes a number max-lines to wordwrap, and then the rest either no
wrapping, or wordwrapping at a different number of characters:
// Wrap the text so it fits within the 35c width (to fit beside the picture)
$text = wordwrap($text, 35, "<br/>");
// The text comes below the picture, where we want wrapping to stop...
$maxLines = 18;
$newWidth = 80;
$lastPos = 1;
for ($i=0;$i<$maxLines;$i++)
{
$lastPos = strpos($text, '<br/>', $lastPos+1);
if ($lastPos === FALSE)
break;
}
$text = substr($text, 0, $lastPos) . "<br/>" .
wordwrap(str_replace('<br/>',' ',substr($text, $lastPos)), $newWidth,
'<br/>');
print $text;
http://php.net/manual/en/function.wordwrap.php