note 19640 added to function.wordwrap

From: Date: Wed, 06 Mar 2002 17:08:05 +0000
Subject: note 19640 added to function.wordwrap
Groups: php.notes 
Request: Send a blank email to php-notes+get-27420@lists.php.net to get a copy of this message
If you are trying to make something like a forum, and you have considered that people my put in LONG words so that the table is stretched out of proportion. Word wrap will work, but doesn’t really do the job intended. With a text box named NEW_MESSAGE submitting the message, this is a possible way to get round it... // Instance of new line (\r\n) with < br > // Note, the received string from a text // area may use different methods of // symbolising the new line $NEW_MESSAGE = str_replace("\r\n", " < br > ", $NEW_MESSAGE); // Split it into the different words, if // there to long word add a space then // put all the words back into a new variable. $SPLIT_NEW_MESSAGE = explode(" ", "$NEW_MESSAGE"); $NUMBER_OF_ELEMENTS = count($SPLIT_NEW_MESSAGE); $COUNT = 0; // Loop though all the words, if one is longer than // 40 characters put in a space, then add this word // to the variable containing the processed string while ($COUNT <= $NUMBER_OF_ELEMENTS) { $CURRENT_WORD = $SPLIT_NEW_MESSAGE[$COUNT]; $LENGTH_OF_WORD = strlen ($CURRENT_WORD); if ($LENGTH_OF_WORD >= 40) { $CURRENT_WORD = wordwrap( $CURRENT_WORD, 40, " ", 1); } $FINAL_NEW_MESSAGE .= "$CURRENT_WORD "; $COUNT = $COUNT + 1; } // Now print out the finished string – you might want to // store it in a database or do some other work to it. Echo “$FINAL_NEW_MESSAGE”; -- http://www.php.net/manual/en/function.wordwrap.php http://master.php.net/manage/user-notes.php?action=edit+19640 http://master.php.net/manage/user-notes.php?action=delete+19640 http://master.php.net/manage/user-notes.php?action=reject+19640

« previous php.notes (#27420) next »