Jump to content

[SOLVED] wordwrap() not wordwrapping


soycharliente

Recommended Posts

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
Share on other sites

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
Share on other sites

  • 3 months later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.