Jump to content


Photo

Limit Shown Text


  • Please log in to reply
4 replies to this topic

#1 Immortal55

Immortal55
  • Members
  • PipPipPip
  • Advanced Member
  • 59 posts

Posted 25 September 2006 - 04:56 AM

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?

#2 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 25 September 2006 - 05:07 AM

Best off doing it in your query. eg;

$sql = "SELECT SUBSTRING(fld,0,20) AS fldname";

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

Of course you could also do it from within php using substr, but there is no point selecting more data than you actually need.

#3 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 25 September 2006 - 05:09 AM

That is much better then what I was going to suggest.. I was going to say use str_split..

Nice 1.

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#4 vbnullchar

vbnullchar
  • Members
  • PipPipPip
  • Advanced Member
  • 428 posts
  • LocationPasig City, Philippines

Posted 25 September 2006 - 05:20 AM

try this 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;
}

sample usage  echo trunc('abcdefghijklmnopqrstuvwxyz', 5);
outputs : abcde...
Registered Linux User #399942
Ubuntu User #14134
--
my blog

#5 Immortal55

Immortal55
  • Members
  • PipPipPip
  • Advanced Member
  • 59 posts

Posted 25 September 2006 - 05:40 AM

Ah, awesome thanks a lot guys.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users