Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Output only 3 lines of text after a fetch. 1

Status
Not open for further replies.
Jun 9, 2004
188
US
Hello. Just a quick question. If I wanted to display only a few lines in mysql fetch array result how would I do this?
My goal is just to show a few lines of the entire article.

Should I use strip? Or something that would only display 100 characters or something.

Thanks guys.

 
Use substr for that.
Code:
$rest = substr($text, 0, 100);
Returns first 100 characters of the string. Of course you might add a few checks to see if the string is at least 100 characters long or check for spaces in order not to cut the text in the middle of the word.
 
Vragabond has the right function here, but I would also attatch "..." at the end, so the user understands that it's been trimmed.

I would not bother to check that it does not cut words, I would simply do:

Before recordset loop:
Code:
// define trim length
$trimlen = 100;

Inside recordset loop (fetch):
Code:
if (strlen($row['text']) > $trimlen)
  {
    $text = substr($row['text'], 0, $trimlen) . "...";
  }
echo "$text <br /><a href=\"?a=read&amp;id={$row['id']}\" title=\"{$row['text']}\">Read entire article</a>";

I hope this makes sence to you :)

sincerely,
Olav ALexander Mjelde
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top