note 4243 modified in ref.strings by maxim
| From: | maxim@php.net | Date: | Tue, 12 Nov 2002 16:13:26 +0000 |
| Subject: | note 4243 modified in ref.strings by maxim | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-39388@lists.php.net to get a copy of this message | ||
I had the problem that in some of the guestbooks I programmed, people entererd trash like 400
characters without spaces and such. Without any spaces there was no way to break the lines and the
whole guestbooked looked screwed. I wrote this little function that makes sure no words are more
than a specified number of characters long. If a word is too long, spaces are inserted to get the
word to the maximum length allowed. Maybe it's useful to some of you:
function strmaxwordlen($input,$len) {
$l = 0;
$output = "";
for ($i = 0; $i < strlen($input); $i++) {
$char = substr($input,$i,1);
if ($char != " ") { $l++; } else { $l = 0; }
if ($l == $len) { $l = 0; $output .= " "; }
$output .= $char;
}
return($output);
}
[Editor's note:
Alternativaly, wordwrap() function is available for this purpose
Maxim]
--was--
I had the problem that in some of the guestbooks I programmed, people entererd trash like 400
characters without spaces and such. Without any spaces there was no way to break the lines and the
whole guestbooked looked screwed. I wrote this little function that makes sure no words are more
than a specified number of characters long. If a word is too long, spaces are inserted to get the
word to the maximum length allowed. Maybe it's useful to some of you:
function strmaxwordlen($input,$len) {
$l = 0;
$output = "";
for ($i = 0; $i < strlen($input); $i++) {
$char = substr($input,$i,1);
if ($char != " ") { $l++; } else { $l = 0; }
if ($l == $len) { $l = 0; $output .= " "; }
$output .= $char;
}
return($output);
}
http://www.php.net/manual/en/ref.strings.php