note 4243 modified in ref.strings by aidan
| From: | aidan@php.net | Date: | Thu, 01 Jul 2004 01:09:42 +0000 |
| Subject: | note 4243 modified in ref.strings by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-72219@lists.php.net to get a copy of this message | ||
[Editor's note: Alternativaly, wordwrap() function is available for this purpose]
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:
<?php
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);
}
?>
--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);
}
[Editor's note:
Alternativaly, wordwrap() function is available for this purpose
Maxim]
http://php.net/manual/en/ref.strings.php