How to shorten a news story for a preview?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

How to shorten a news story for a preview?

Post 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
User avatar
Wayne
Forum Contributor
Posts: 339
Joined: Wed Jun 05, 2002 10:59 am

Post 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.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

yes, the news is being pulled from a MySQL Database.

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

Mark
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Looks good twig!

Thanks

Mark
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post 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]
Post Reply