Jump to content

message resize from database


fxuser

Recommended Posts

Hello, i have a textarea which can contains too long sentences in a row and many new lines

 

i have a div which is auto height and retrieve each message from the textarea .. the thing is that i want to resize it so it can fit inside the width of the dive which is about 700px .. i tried getting all sentences between each newline and resize then if they are >80 chars and then add <br />

 

but the login doesnt seem correct..

 

how else can i achieve that?

 

thanks.

Link to comment
Share on other sites

Without seeing your code we really can't help you. Also, I'm not sure what "login" has to do with your problem.

 

Anyway, here is a small function that will truncate a line of text to a specified number of characters (if needed). It will only truncate on whole words, so if the max characters falls within a word it will truncate to the previous word. Also, there is an optional parameter to add ellipses (or any other value) to the end of the text if it is trucated:

 

//Return a string up to a certain number of characters
//but will break on a space
function truncateString($string, $maxLength, $ellipse='...')
{
    if (strlen($string) <= $maxLength) { return $string; }
    return array_shift(explode("\n", wordwrap($string, $maxLength))) . $ellipse;
}

Link to comment
Share on other sites

Without seeing your code we really can't help you. Also, I'm not sure what "login" has to do with your problem.

 

Anyway, here is a small function that will truncate a line of text to a specified number of characters (if needed). It will only truncate on whole words, so if the max characters falls within a word it will truncate to the previous word. Also, there is an optional parameter to add ellipses (or any other value) to the end of the text if it is trucated:

 

//Return a string up to a certain number of characters
//but will break on a space
function truncateString($string, $maxLength, $ellipse='...')
{
    if (strlen($string) <= $maxLength) { return $string; }
    return array_shift(explode("\n", wordwrap($string, $maxLength))) . $ellipse;
}

 

i wanted to say logic*

 

so i submit the form and add to the database the text(stored as varchar 10k chars) dont know if thats a problem , maybe i could save it as text?

then retrieve the text with:

 

$content = str_replace("\n","<br />",$content);

so it will replace \n to <br> and then i get smth like this:

118m684.png

http://i51.tinypic.com/118m684.png

what i want is to shrink it and make its width fit into the gray div

Link to comment
Share on other sites

How you store the data is not the problem. When retrieveing the text from the database, you *could* partially truncate it using MySQL substring function (getting it slightly larger than what the maximum is you would allow) and then use PHP code to further refine that output. That *might* be more efficient since not as much data would have to be managed in memory. I don't know, but it might be worth testing.

 

But, no matter if you do that or not, you are going to want to use PHP to determine the final output. The problem you have, however, is that PHP cannot determine how wide the text will be displayed on the page (see excpetion below). The number of characters is not definitive in determining this since each character has a different width: a "W" is much wider than the letter "i". One workaround is to use a fixed-width font, but that is not a pleasing display to the user

With a fixed width font
every character has the same width

 

So, you can go with a fixed-width font and determine how many characters you want to display on a line or you have to come up with some limit of characters that you think will apply to 99% of input using a non fixed-width font.

 

And, as for this

$content = str_replace("\n","<br />",$content);

 

That is NOT what you want to do, because a line break is treated differently based upon the server. Could be "\n", "\r" or "\n\r". That is why PHP has the function nl2br(); which will convert line breaks into BR tags for you.

$content = nl2br($content);

 

Link to comment
Share on other sites

ok it seems that i can get it to work but the problem is with the spam texts

 

like:

 

ssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss

 

in a single line which may be bigger and get out of the div... which i cant add <br />'s

 

what i got is:

	$txt= nl2br($txt);
	$new_text = wordwrap($txt, 80, "<br />\n");
	//and echo the new text
	echo $new_text;

Link to comment
Share on other sites

There is an easy answer regarding very long words. Take a look at the manual for wordwrap() - specifically the 4th parameter! http://php.net/manual/en/function.wordwrap.php

 

However, that code will not work as you intend it to. the wordwrap() function doesn't take into consideration existing line breaks. So, if your first line has 64 characters and a pre-existing line break (6 characters), the wordwrap function will attempt to add a line break after the 10th character on the next line. What I think you want is to only wrap lines that are greater than the width, but leave existing lines that already have line breaks alone. The only way I can think of to do that is to process the text line by line.

 

There may be a more efficient way to do this, but I think this will do as you are looking for

function wordWrapPreserve($inputText, $maxWidth)
{
    $inputLines = explode('<br />', nl2br($inputText));
    foreach($inputLines as $key => $line)
    {
        $inputLines[$key] = wordwrap($line, $maxWidth, "<br>\n", true);
    }
    $outputText = implode("<br />\n", $inputLines);
    return $outputText;
}

//Usage
echo  wordWrapPreserve($text, 80);

Link to comment
Share on other sites

Actually i solved it as i marked the topic but thats what i do for others who might have the same problem,

 

i just get my text from database , add nl2br function to it and then add a style with word-wrap:break-word; and white-space:pre-wrap;

 

and it pretty much solve the problem.

 

thanks for the help though. the functions above covered some other cases i wanted.

Link to comment
Share on other sites

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.