Jump to content

Archived

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

Immortal55

Limit Shown Text

Recommended Posts

Alright, lets say I have a LONGTEXT field in my DB, and something long is entered in there.

And on a page i only want to show......lets say 25 characters, how would I limit it to only showing 25 characters?

Share this post


Link to post
Share on other sites
Best off doing it in your query. eg;

[code]
$sql = "SELECT SUBSTRING(fld,0,20) AS fldname";
[/code]

this would select the first 20 chars from the field called fld.

Of course you could also do it from within php using [url=http://php.net/substr]substr[/url], but there is no point selecting more data than you actually need.

Share this post


Link to post
Share on other sites
That is much better then what I was going to suggest.. I was going to say use str_split..

Nice 1.

Share this post


Link to post
Share on other sites
try this code

[code]function trunc($details,$max) {
if(strlen($details)>$max) {
        $details = substr($details,0,$max);
        $i = strrpos($details," ");
        $details = substr($details,0,$i);
        $details = $details."...";
    }
    return $details;
}[/code]

sample usage  echo trunc('abcdefghijklmnopqrstuvwxyz', 5);
outputs : abcde...

Share this post


Link to post
Share on other sites

×

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.