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; } } } ?> Link to comment https://forums.phpfreaks.com/topic/59505-solved-wordwrap-not-wordwrapping/ 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 Link to comment https://forums.phpfreaks.com/topic/59505-solved-wordwrap-not-wordwrapping/#findComment-295705 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? Link to comment https://forums.phpfreaks.com/topic/59505-solved-wordwrap-not-wordwrapping/#findComment-295707 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 Link to comment https://forums.phpfreaks.com/topic/59505-solved-wordwrap-not-wordwrapping/#findComment-295711 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? Link to comment https://forums.phpfreaks.com/topic/59505-solved-wordwrap-not-wordwrapping/#findComment-295721 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 Link to comment https://forums.phpfreaks.com/topic/59505-solved-wordwrap-not-wordwrapping/#findComment-295729 Share on other sites More sharing options...
brent123456 Posted November 4, 2007 Share Posted November 4, 2007 Thanks! This worked awesome! Link to comment https://forums.phpfreaks.com/topic/59505-solved-wordwrap-not-wordwrapping/#findComment-384512 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.