May
22
2011

Truncating read more text by words not length

I have found that truncating a string by it length is not always an elegant solution. To give your site a more polished look try truncating by whole words instead.

So instead of getting something like:
“This is my awesome paragraph. It is really long and I am making it shor… read more”

You will get:
“This is my awesome paragraph. It is really long and I am making it shorter… read more”

$orig_str = strip_tags($your_string);
$new_str = implode(' ',array_slice(explode(' ',strip_tags($your_string)),0,40));
if(strlen($orig_str)>strlen($new_str)){
	$pub_str = $new_str.'... <a href="/about-tim-wickstrom/">read more</a>';
}else{
	$pub_str= $orig_str;
}
echo $pub_str;

3 Comments + Add Comment

  • how to use? no examples can we learn ?

    Thumb up 0 Thumb down 0

  • The first time I’ve ever clicked on “I’m Feeling Lucky” and finally got a useful website. Thank you moncler!

    Thumb up 0 Thumb down 0

  • I’ve been browsing online more than 3 hours today, yet I never found
    any interesting article like yours. It’s pretty worth enough for me.
    In my view, if all web owners and bloggers made good content as you did, the net
    will be much more useful than ever before.

    Thumb up 0 Thumb down 0

Leave a comment

Show/Hide Footer Actions

Status: Available for your project.