Danny159 (20th January 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

PHP: Trim a String Without Cutting Any Words
(I make no assertion about the licensing position for that snippet, check before using.)
Danny159 (20th January 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
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-
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.PHP Code:IF (strlen($row['URL']) > 30) { $url=substr($row['URL'],0,30) . '...';} else {$url=$row['URL'];};
For your reference should anyone be looking to do this, here is the code to display the URL later on-
If you have any questions about how any of this works please let me knowPHP Code:echo '<div id="itemurl" class="list">Website: <a href="' . $row['URL'] . '" target="_blank">' . $url . '</a></div>';
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 09:38 PM. Reason: Forgot to add offset to strpos

$string = substr($string, 0, 340);
$string = substr($string, 0, strrpos($string, ' '));
Then add '...'
Echo $string."...";
I use the following function then I can set how many words I want to have in the text.
Then to call the function use:PHP Code:function limit_words($string, $word_limit) {
$words = explode(" ",$string);
return implode(" ",array_splice($words,0,$word_limit));
}
Works really well for the web app we have running here.PHP Code:limit_words($variable,20);
Danny159 (21st January 2010)
Thanks for all your help guys! All sorted now
Dan
There are currently 1 users browsing this thread. (0 members and 1 guests)