note 29062 deleted from function.htmlspecialchars by maxim

From: Date: Fri, 31 Jan 2003 21:22:25 +0000
Subject: note 29062 deleted from function.htmlspecialchars by maxim
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-43223@lists.php.net to get a copy of this message
Here's a functon to calculate the number of rows a piece of text is going to take up in a textarea field in a form. This means that you can eliminate scroll bars on the field when displaying the text. It works well with IE, but Netscape tends to add a few extra colums for free, so the box may be ever so slightly too large. Example usage: $cols = 64; $rows = calc_rows($note_string, $cols); echo "<textarea rows=$rows cols=$cols Name=\"note\">"; echo htmlspecialchars($note_string); echo "</textarea>"; function calc_rows($string, $cols) { $rows = 1; $chars_on_line = 0; $chars_in_word = 0; $str_len = strlen($string); for ($i = 0; $i < $str_len; $i++) { switch ($string[$i]) { case " ": $chars_in_word = 0; $chars_on_line ++; break; case "\r": break; case "\n": $rows ++; $chars_in_word = 0; $chars_on_line = 0; break; case "\t": $chars_in_word = 0; $chars_on_line = $chars_on_line + 8 - ($chars_on_line % 8); break; default: $chars_in_word ++; $chars_on_line ++; if ($chars_on_line >= $cols) { $rows ++; // Wrap the current word onto a new line // (unless the new word would fill the line anyway) if ($chars_on_line > $chars_in_word) $chars_on_line = $chars_in_word; else $chars_on_line = 0; $chars_in_word = 0; } break; } } return $rows; }

« previous php.notes (#43223) next »