note 4243 modified in ref.strings by sander
| From: | sander@php.net | Date: | Sun, 07 Apr 2002 13:36:48 +0000 |
| Subject: | note 4243 modified in ref.strings by sander | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-28914@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);
}
--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:
<p>
function strmaxwordlen($input,$len) {<br>
$l = 0;<br>
$output = "";<br>
for ($i = 0; $i < strlen($input); $i++) {<br>
$char = substr($input,$i,1);<br>
if ($char != " ") { $l++; } else { $l = 0; }<br>
if ($l == $len) { $l = 0; $output .= " "; }<br>
$output .= $char;<br>
}<br>
return($output);<br>
}<br>
http://www.php.net/manual/en/ref.strings.php