note 19640 deleted from function.wordwrap by danbrown

From: Date: Sun, 17 May 2009 18:40:32 +0000
Subject: note 19640 deleted from function.wordwrap by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-155163@lists.php.net to get a copy of this message
Note Submitter: krang at krang dot org dot uk ---- 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”;

« previous php.notes (#155163) next »