note 23223 added to function.wordwrap
| From: | toxic79_spam at yahoo dot com | Date: | Fri, 12 Jul 2002 00:02:25 +0000 |
| Subject: | note 23223 added to function.wordwrap | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-32941@lists.php.net to get a copy of this message | ||
I've noticed that if you have a string of, say, 8 characters and you set it to wrap at 8
characters with a break tag it will add the break tag after the 8th character even if the string is
only 8 characters long. this was frustrating because I only wanted the break tag if there would be
more text after it. it would be cool to have a flag that would stop this from happening, or a flag
that would put the break tag (or whatever you use) before the number you break on, so you could set
it to break every 9th character and it would only insert the tag after the 8th if there are more
than 8 characters.
until then, however, here is my lame fix. $str is the string to wrap, $num is the number of
characters to wrap after and $break is the text to use to wrap the string.
function my_wordwrap( $str, $num, $break )
{
// get the wordwrapped string
$tmp_str = wordwrap( trim( $str ), $num, $break, 1 );
// get the string length of the breaking tag
$strlen_break = strlen( $break );
// if the last $strlen_break characters of the wordwrapped string is $break ...
if ( substr( $tmp_str, ( 0 - $strlen_break )) == $break )
{
// strip the last $strlen_break characters off the end
$tmp_str = substr( $tmp_str, 0, count( $tmp_str ) - $strlen_break - 1 );
}
// return the results
return $tmp_str;
}
--
http://www.php.net/manual/en/function.wordwrap.php
http://master.php.net/manage/user-notes.php?action=edit+23223
http://master.php.net/manage/user-notes.php?action=delete+23223
http://master.php.net/manage/user-notes.php?action=reject+23223