开发者

How to crop a text that it can fit into a block-type tag?

In my form I'm asking users to enter a text and then when they submit the form the text is sent to my database. When I query the entered text and I insert it into my html page it doesn't fit the container instead if a line has to many words it outputs the line in length. I'd like to know how can I do to crop the line if its size is way too much assuming the size of the container in which it reads. and oh ! to extend my request : what is the best way to treat user input and retrieve it in the 开发者_如何学Cexactly same format ??


If you need to "crop" text you can simply use substr.

echo substr($string,0,150);

This will cut your string up to 150 chars

@OP: after reading your question. Did you mean the css ?

overflow: hidden;

Anyway even if you div is set to display:block; it shouldn't show the horizontal scrollbar

Addendum

The problem with your link is that your div has the class listing with

white-space: pre;

you should change it

white-space: normal;


This is not a perfect solution, but you could do it like this:

$string = 'yourstring';

if (strlen($string) > TheMaxSizeOfYourString) {
    $string = substr($string, 0, TheMaxSizeOfYourString-3);
    $string .= '...';
}

This isn't perfect as the actual width of your string varies on the font you use.


Not sure what the problem is. If you specify the maxlength of a text input, you'll always have a set max length of your text. Otherwise you can do substr($yourstring, 0, your_set_length). However simply setting maxlength makes more sense.


you can shorten text to a specific length and 'round' it to the nearest full word by using something like i've got below, and even have it add some trailing "..." if needed...

function ShortenText($text,$chars) {
        // Change to the number of characters you want to display
        $orig = $text;
        $text = $text." ";
        $text = substr($text,0,$chars);
        $text = substr($text,0,strrpos($text,' '));

        // Add ... if the text actually needs shortening
        if (strlen($orig) > $chars) {
            $text = $text."...";
        }
        return $text;

}


If the only problem related to formating is the new lines then I would go like this:

  1. User enters some text in text area.

  2. I clean the text which is I remove all new lines, (also if you like remove tabs or multiple spaces).

  3. Store the clean text in the database.

  4. When the text will be retrieved then you output it in html page, if you place it inside a div then you don't have to worry because the text will break on its own.

  5. If you need to show a preview of it you choose the substr as indicated above.

Conclusion I don't think that your request to get the text in excactly same format is a good idea (except that you have sth unusual-else in your mind). In contrast you want to clean the text that the user inserts. Hope I have understood correct what you are trying to do!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜