Page 1 of 1

How to shorten a news story for a preview?

Posted: Mon Apr 05, 2004 4:47 am
by JayBird
Okay, i have a news section on a site that has a news sections.

What i want to do is show a shortend preview of the story.

Say i had this text...

Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.

i want to shorten it so it looks like this

Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt...

It must not cut off half way through a word.

I thought about using explode() then itterating through the array a set number of times depending on how many words i want to display in the preview.

Is this the best way?

Thanks

Mark

Posted: Mon Apr 05, 2004 5:07 am
by Wayne
what about something like ....

Code: Select all

substr($text,0,strpos($text, " ", "100"))
... replace 100 with whatever value you would like the news length to be, should find the next space.

Posted: Mon Apr 05, 2004 5:42 am
by twigletmac
If you've got the news in a database there may be functions within that database that allow you to do this (I know MySQL has one whose name I can't remember).

Mac

Posted: Mon Apr 05, 2004 6:17 am
by JayBird
yes, the news is being pulled from a MySQL Database.

If you can rememebr the function, holla at me :)

Mark

Posted: Mon Apr 05, 2004 6:25 am
by twigletmac
Had a quick squiz at the string functions and I think it is probably SUBSTRING_INDEX() that you need:
http://www.mysql.com/doc/en/String_func ... ml#IDX1282

You can use a space as the delimeter and set the number of words you want. Saves taking info out the db that you don't need.

Mac

Posted: Mon Apr 05, 2004 6:26 am
by JayBird
Looks good twig!

Thanks

Mark

Posted: Mon Apr 05, 2004 7:52 am
by malcolmboston
bech wrote: What i want to do is show a shortend preview of the story.
ive also been wondering this in the past few days but have'nt got upto that part of the app yet, thanks :D[/quote]