Danny159 Posted January 20, 2010 Posted January 20, 2010 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
powdarrmonkey Posted January 20, 2010 Posted January 20, 2010 PHP: Trim a String Without Cutting Any Words (I make no assertion about the licensing position for that snippet, check before using.) 1
LosOjos Posted January 20, 2010 Posted January 20, 2010 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
pwds Posted January 20, 2010 Posted January 20, 2010 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- 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- echo 'Website: ' . $url . ''; If you have any questions about how any of this works please let me know
powdarrmonkey Posted January 20, 2010 Posted January 20, 2010 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- IF (strlen($row['URL']) > 30) { $url=substr($row['URL'],0,30) . '...';} else {$url=$row['URL'];}; Ye-eahh....maybe you should read the OP again
pwds Posted January 20, 2010 Posted January 20, 2010 (edited) 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 = ?? } Edited January 20, 2010 by pwds Forgot to add offset to strpos
danIT Posted January 20, 2010 Posted January 20, 2010 $string = substr($string, 0, 340); $string = substr($string, 0, strrpos($string, ' ')); Then add '...' Echo $string."...";
chris_uk Posted January 20, 2010 Posted January 20, 2010 I use the following function then I can set how many words I want to have in the text. function limit_words($string, $word_limit) { $words = explode(" ",$string); return implode(" ",array_splice($words,0,$word_limit)); } Then to call the function use: limit_words($variable,20); Works really well for the web app we have running here. 1
Danny159 Posted January 21, 2010 Author Posted January 21, 2010 Thanks for all your help guys! All sorted now Dan
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now