Tandem Posted April 27, 2007 Share Posted April 27, 2007 Hi, my site features a lot of user input, such as user profiles, and some forums that i wrote myself as well as many other similar examples. My problem is that if somebody enters a very long word, wider than the page width, it sort of screws up the page design, and generally causes me some annoyance whilst performing certain tasks. I'm wondering if there is a way to detect whether a string has any words longer than a specified length in it? I was thinking that perhaps using explode(" ", $variable), and then looping through the array and making sure that there are no words of longer than the length i want, but i can see that being quite slow Any suggestions or links to any help are appreciated. Thanks. Link to comment https://forums.phpfreaks.com/topic/49011-words-of-a-certain-length/ Share on other sites More sharing options...
per1os Posted April 27, 2007 Share Posted April 27, 2007 [quote=www.php.net/wordwrap] Peter 19-Dec-2006 05:00 The main concern when you have a text in a cell is for long words that drags the cell margins. This function will break words in a text that have more then $nr characters using the "-" char. function processtext($text,$nr=10) { $mytext=explode(" ",trim($text)); $newtext=array(); foreach($mytext as $k=>$txt) { if (strlen($txt)>$nr) { $txt=wordwrap($txt, $nr, "-", 1); } $newtext[]=$txt; } return implode(" ",$newtext); } <?php function processtext($text,$nr=10) { $mytext=explode(" ",trim($text)); $newtext=array(); foreach($mytext as $k=>$txt) { if (strlen($txt)>$nr) { $txt=wordwrap($txt, $nr, "-", 1); } $newtext[]=$txt; } return implode(" ",$newtext); } ?> Shouldn't be slow at all. I bet it can process 10,000 words fairly quickly. Give it a test and see if that works out for you. Link to comment https://forums.phpfreaks.com/topic/49011-words-of-a-certain-length/#findComment-240077 Share on other sites More sharing options...
Tandem Posted April 27, 2007 Author Share Posted April 27, 2007 Thanks for that, i'll give it a try. Link to comment https://forums.phpfreaks.com/topic/49011-words-of-a-certain-length/#findComment-240113 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.