SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict Raioo's Avatar
    Join Date
    Jul 2000
    Location
    Boston
    Posts
    212
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello,

    I have this article that's like 300 words. It is stored in a table in database mysql. I need to display only a few words from this article in the front page then print "read full article"

    is there a specific function in PHP that allows me to give it the string variable (the whole article after it's been queried from the db) , and... the number of spaces to count before stopping so that I can tell it something like:

    $article_motivation = give_me_few_words( $article, " ", 20 );

    meaning look for 20 spaces (.. 20 words..) and stop and return me that string.

    please help,
    Thanks,
    Adnane
    I appreciate the help of everyone at SitePoint.com

  2. #2
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can do it in the query,

    Code:
    select field1, substring_index(fieldtotruncate, ' ', 20) as shortfield from tablename;
    Change 20 to be how ever many words you want. and yes that is a space between the single quotes. "fieldtotruncate" should be the mysql fieldname of the column where the data is.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  3. #3
    Serial Publisher silver trophy aspen's Avatar
    Join Date
    Aug 1999
    Location
    East Lansing, MI USA
    Posts
    12,937
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    to add on to what freddy said

    Its better to do it in the query itself as if you did it with the PHP MySQL would have to send you the full article - this way you only get sent the part you need.
    Chris Beasley - I publish content and ecommerce sites.
    Featured Article: Free Comprehensive SEO Guide
    My Guide to Building a Successful Website
    My Blog|My Webmaster Forums


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •