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
https://forums.phpfreaks.com/topic/59505-solved-wordwrap-not-wordwrapping/
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

  • 3 months later...

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.