Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Oct 2006
    Location
    Bristol
    Posts
    128
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Showing only the first part of an article

    Hi, I want to get an article(just some text) from a mysql database but only show the first 10 words or so. How would I go about doing this?

    thanks
    Alex

  • #2
    New Coder
    Join Date
    Oct 2006
    Posts
    82
    Thanks
    3
    Thanked 0 Times in 0 Posts
    In future I would suggest you have a summary and then a main content field in the database, then you wouldn't need to chop things up as you could just display the summary. Not that I know what you want to do though

    Anyway, assuming you know how to pull the field from the database (if you don't then just post again) this is what you could do...

    PHP Code:

    <?php

    echo substr($article030);

    ?>

    That would show the first 30 characters (which would include spaces). I'm not sure the best way to go about actual words, probably to use the 'explode' function, but if you're not too bothered about the number of words and are more concerned with having roughly the same amount of text then that should do you!

  • #3
    New Coder
    Join Date
    Oct 2006
    Posts
    82
    Thanks
    3
    Thanked 0 Times in 0 Posts
    I can see where my first method would cause problems. If you just set it to a fixed length of characters then you may only return part of the last word shown.

    In which case I propose you use the explode function.
    PHP Code:

    <?php

    $word 
    explode(" "$article);

    echo 
    $word[0]." ".$word[1]." ".$word[2]." ".$word[3]." ".$word[4]." ".$word[5];

    ?>
    You get the idea? That should display the first 6 words of the article, and it recognises separate words when it comes across a space " " as outlined in the explode statement.

    Hope that has been of some help.

  • #4
    New Coder
    Join Date
    Oct 2006
    Posts
    82
    Thanks
    3
    Thanked 0 Times in 0 Posts
    One more technique which does the same as above but will save you some time...

    PHP Code:
    <?php


    $word 
    explode(" "$article);

    for(
    $i 0$i strlen($article) ; $i++)
            {
                echo 
    $word[$i]." ";
                
            }
    ?>


  •  

    Posting Permissions

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