soycharliente Posted July 11, 2007 Share Posted July 11, 2007 I've created a basic forum, just to do it. I'm trying to use wordwrap() so that when a user enters a title or a post they can't put a ton of characters in a row and cause the layout to "break". This is the code I'm using and the wordwrap() part isn't functioning. Can anyone help? One section: <?php if (isset($_POST["submit_reply"])) { $parentid = $_POST["parentid"]; $author = getUserId($_SESSION["user"]); $reply = $_POST["reply"]; $error_reply = (empty($reply)) ? TRUE : FALSE; $error_replylength = (strlen($reply) >= 5000) ? TRUE : FALSE; if (!($error_reply||$error_replylength)) { dbconnect(); $reply = myEscape($reply); $nreply = wordwrap($reply, 70, "<br />\n"); $result = mysql_query("INSERT INTO posts VALUES (NULL, '$parentid', '$author', NOW(), '$nreply')"); if ($result) { header("Location: showThread.php?id=$parentid"); exit; } } } ?> Quote Link to comment Share on other sites More sharing options...
OLG Posted July 11, 2007 Share Posted July 11, 2007 Give this function a whirl instead <?php function breakLongWords($str, $maxLength, $char){ $wordEndChars = array(" ", "\n", "\r", "\f", "\v", "\0"); $count = 0; $newStr = ""; $openTag = false; for($i=0; $i<strlen($str); $i++){ $newStr .= $str{$i}; if($str{$i} == "<"){ $openTag = true; continue; } if(($openTag) && ($str{$i} == ">")){ $openTag = false; continue; } if(!$openTag){ if(!in_array($str{$i}, $wordEndChars)){//If not word ending char $count++; if($count==$maxLength){//if current word max length is reached $newStr .= $char;//insert word break char $count = 0; } }else{//Else char is word ending, reset word char count $count = 0; } } }//End for return $newStr; } ?> Use it exactly the same way Quote Link to comment Share on other sites More sharing options...
soycharliente Posted July 11, 2007 Author Share Posted July 11, 2007 Is the 3rd param what char I want to separate with? Quote Link to comment Share on other sites More sharing options...
OLG Posted July 11, 2007 Share Posted July 11, 2007 That is correct wordwrap($reply, 70, "<br />\n"); ^^ exactly the same usage as the function above Quote Link to comment Share on other sites More sharing options...
soycharliente Posted July 11, 2007 Author Share Posted July 11, 2007 Worked very nicely. Thanks. You write that yourself? Quote Link to comment Share on other sites More sharing options...
OLG Posted July 11, 2007 Share Posted July 11, 2007 No, i took it straight from the comments section on the PHP.net manual reference from wordwrap - I've used it before, so i knew it worked Quote Link to comment Share on other sites More sharing options...
brent123456 Posted November 4, 2007 Share Posted November 4, 2007 Thanks! This worked awesome! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.