-1

I'm working on a blog site and want to display list of blogs on the first page. Then user click on title to read more about it.

But I'm not getting how do I show few lines of the blogs. As, the blog text posted contains html elements, and which might have an image too.

I'm thinking of reading the text from the database and return only first 5 to 10 lines.
How could I read from a string line by line in php?

Any other suggestion would be appreciated.
Thanks

EDIT
this answer says about reading using file() as array from file but I've to read it from database as a string.

Community
  • 1
  • 1
Mohammad Faisal
  • 5,783
  • 15
  • 70
  • 117

2 Answers2

1

Instead of trying to do something complex, why not just fetch all the data you need and then show what you want with css, e.g.

.blogEntry {
  width: 600px;
  height: 50px;
  overflow: hidden;
}

You can add a text-overflow: ellipsis; to show there's more. If you want to hide images in the preview then just set css for that as well, e.g.

.blogEntry img { display: none; }

If you do only want to fetch part of it though (again, if you need to use the full text after on the same page you might as well just fetch it all then) you can do it with mysql, e.g. if you wanted to select the first 100 characters:

SELECT LEFT(yourfield, 100) AS excerpt FROM table(s) WHERE ...
spacebean
  • 1,554
  • 1
  • 10
  • 13
0

If they are seperated by a character (e.g. /), you can look at explode() and add them one by one.

If you want to read it from a database, try LIMIT 10.

Realitätsverlust
  • 3,941
  • 2
  • 22
  • 46