+ Post New Thread
Results 1 to 10 of 10
Web Development Thread, [PHP] Shortening Text from DB in Coding and Web Development; Hi, I have designed a website that takes news posts from a database and echos them on the news page, ...
  1. #1

    Join Date
    Sep 2008
    Location
    Oakham
    Posts
    31
    Thank Post
    14
    Thanked 1 Time in 1 Post
    Rep Power
    0

    [PHP] Shortening Text from DB

    Hi,

    I have designed a website that takes news posts from a database and echos them on the news page, however I am trying to shorten the text using php and add ... at the end.

    So taking the main long post and then taking the first 340 letters and only showing that so people have to click the read more... but I can get it to shorten however sometimes it just cuts words up...

    Does anyone know how to tell it to not chop words off!!

    Dan

  2. #2

    powdarrmonkey's Avatar
    Join Date
    Feb 2008
    Location
    Alcester, Warwickshire
    Posts
    4,855
    Thank Post
    412
    Thanked 777 Times in 650 Posts
    Rep Power
    181
    PHP: Trim a String Without Cutting Any Words

    (I make no assertion about the licensing position for that snippet, check before using.)

  3. Thanks to powdarrmonkey from:

    Danny159 (20th January 2010)

  4. #3

    LosOjos's Avatar
    Join Date
    Dec 2009
    Location
    West Midlands
    Posts
    5,182
    Thank Post
    1,285
    Thanked 1,029 Times in 729 Posts
    Rep Power
    658
    Haven't used PHP in a long time so I can't provide code but it should be simple enough, just find the next space after the limit you're setting on string length and add your '...' in there

  5. #4
    pwds's Avatar
    Join Date
    Dec 2008
    Location
    Derby
    Posts
    279
    Thank Post
    73
    Thanked 48 Times in 38 Posts
    Rep Power
    19
    Here's some code I used to shorten URL's retrieved from a database if the URL exceeds 30 characters and add three dots in that event to indicate the shortening-

    PHP Code:
    IF (strlen($row['URL']) > 30) { $url=substr($row['URL'],0,30) . '...';} else {$url=$row['URL'];}; 
    I find shortened fields work best if you have a maximum length as otherwise you are shortening data that could easily be displayed on the page.

    For your reference should anyone be looking to do this, here is the code to display the URL later on-

    PHP Code:
    echo '<div id="itemurl" class="list">Website: <a href="' $row['URL'] . '" target="_blank">' $url '</a></div>'
    If you have any questions about how any of this works please let me know

  6. #5

    powdarrmonkey's Avatar
    Join Date
    Feb 2008
    Location
    Alcester, Warwickshire
    Posts
    4,855
    Thank Post
    412
    Thanked 777 Times in 650 Posts
    Rep Power
    181
    Quote Originally Posted by pwds View Post
    Here's some code I used to shorten URL's retrieved from a database if the URL exceeds 30 characters and add three dots in that event to indicate the shortening-

    PHP Code:
    IF (strlen($row['URL']) > 30) { $url=substr($row['URL'],0,30) . '...';} else {$url=$row['URL'];}; 
    Ye-eahh....maybe you should read the OP again

  7. #6
    pwds's Avatar
    Join Date
    Dec 2008
    Location
    Derby
    Posts
    279
    Thank Post
    73
    Thanked 48 Times in 38 Posts
    Rep Power
    19
    Apologies, the only way I can think of is to have conditional statements to look for spaces so many characters in to the string, find the first space after the approx number of characters you want to cut at ($offset), then use that as a variable in the statements I posted above. So the defined 30 characters there would be substituted for something like $nochars

    $nochars = strpos($data, " ", $offset);

    If it's finding the position of the space you may wish to take one off that value to have the dots follow directly on from the last word, although this is probably a matter of personal taste.

    Obviously this won't break at carriage returns and you should have a statement to check if $nochars doesn't return a value in case of a single word entry or SomethingThatRandomlyDoesn'tHaveSpaces.

    e.g.

    if ($nochars === false) {
    $nochars = ??
    }
    Last edited by pwds; 20th January 2010 at 08:38 PM. Reason: Forgot to add offset to strpos

  8. #7

    webman's Avatar
    Join Date
    Nov 2005
    Location
    North East England
    Posts
    8,372
    Thank Post
    624
    Thanked 949 Times in 651 Posts
    Blog Entries
    2
    Rep Power
    317

  9. #8

    Join Date
    Apr 2006
    Location
    UK
    Posts
    939
    Thank Post
    39
    Thanked 70 Times in 54 Posts
    Rep Power
    29
    $string = substr($string, 0, 340);
    $string = substr($string, 0, strrpos($string, ' '));

    Then add '...'

    Echo $string."...";

  10. #9

    Join Date
    Jan 2008
    Posts
    144
    Thank Post
    12
    Thanked 27 Times in 25 Posts
    Rep Power
    19
    I use the following function then I can set how many words I want to have in the text.
    PHP Code:
    function limit_words($string$word_limit) {
        
    $words explode(" ",$string);
        return 
    implode(" ",array_splice($words,0,$word_limit));

    Then to call the function use:
    PHP Code:
    limit_words($variable,20); 
    Works really well for the web app we have running here.

  11. Thanks to chris_uk from:

    Danny159 (21st January 2010)

  12. #10

    Join Date
    Sep 2008
    Location
    Oakham
    Posts
    31
    Thank Post
    14
    Thanked 1 Time in 1 Post
    Rep Power
    0
    Thanks for all your help guys! All sorted now

    Dan

SHARE:
+ Post New Thread

Similar Threads

  1. Convert Text On Image Into Editable Text
    By DaveP in forum General Chat
    Replies: 4
    Last Post: 30th May 2013, 09:10 AM
  2. PHP to text file
    By Hightower in forum Web Development
    Replies: 21
    Last Post: 24th October 2008, 09:52 AM
  3. Marquee Text
    By Silverman in forum Web Development
    Replies: 5
    Last Post: 25th June 2008, 01:26 PM
  4. Old Text Messages
    By triggmiester in forum Netbooks, PDA and Phones
    Replies: 12
    Last Post: 10th June 2008, 01:07 PM
  5. Need help moving text
    By jamin100 in forum EduGeek Joomla 1.0 Package
    Replies: 5
    Last Post: 2nd April 2008, 01:40 AM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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