Question

I'm writing a blog and I want to show short versions of posts on the main page. I assume native php string functions aren't appropriate here since posts can be large and it would take long to substr all posts in loop. So, what is the common strategy here? I hope the question is clear and specific.

I don't want to shorten posts on client side with JS, that's not an option.

Was it helpful?

Solution

The solution I use is to make another field in db table with posts where I put short version of post, cuted begining or something like that.

It's faster, and better, you don't have to worry about length becouse you control it, there is no problem with evenual html tags used in context, and you can have a bit diffrent text on mainpage

OTHER TIPS

I can think of two options. The first one involves you writing excerpts for your blog posts manually. Doing this, you don't have to worry about PHP at all.

If you do want to go ahead and automatically generate excerpts, I would set a upper character limit and then cut at the end of the sentence nearest the chosen limit. This approach may or may not produce good results depending on how your post is written.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top