Is there a function in php or css to stop people posting huge long words. If someone posts a word on my forum that has a huge amount of charaters then it will keep going on one line forever, can this be stopped? Thanks
Try this PHP code: $char_limit = 30; // number of characters allowed $words = preg_split("/[\s,]+/", $text); if (is_array($words)) foreach ($words as $v) if (strlen($v) > $char_limit) { // do something because a word has more than specified number of characters. } PHP: Best o' luck.
Here goes, as a function.. a little sloppy, but it works: // Usage: <?= long_word_spacer($comment_text, $character_limit); ?> function long_word_spacer($text, $char_limit = 24) { $words = preg_split("/[\s,]+/", $text); if (is_array($words)) foreach ($words as $v) { $r = ""; if (strlen($v) > $char_limit) { for ($i = 0; $i < strlen($v); $i++) { if ($c++ == $char_limit) { $r .= $v[$i]." "; $c = 0; } else $r .= $v[$i]; } $text = str_replace($v, $r, $text); } } return $text; } PHP: You can replace the " " with a "<br>" or "\n" if you feel so inclined.
I totally forgot about PHP's word_wrap function (http://us2.php.net/wordwrap). Check out this cleaner variation of the same function: function long_word_spacer($text, $char_limit = 24) { $words = preg_split("/[\s,]+/", $text); if (is_array($words)) { foreach ($words as $k => $v) { if (strlen($v) > $char_limit) $v = wordwrap($v, $char_limit, " ", true); $r .= $v." "; } return $r; } else { return wordwrap($text, $char_limit, " ", true); } } PHP: There you go.
function word_split($text, $char_limit = 24) { $words = str_word_count($text, 1); foreach ($words as $v) { $r .= (strlen($v) > $char_limit)? wordwrap($v, $char_limit, ' ', true).' ':$v.' '; }//end for loop return $r; }//end word_split function PHP: