Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Limit number of words pulled from DB
Old 08-23-2010, 08:58 PM Limit number of words pulled from DB
Truly's Avatar
Ultra Talker

Posts: 322
Trades: 0
Hey all,

I have come up with some pretty outlandish solutions to this problem, but I feel like there has to be something elegant and simple out there.

I can easily cut a string after X# of characters, but whats the best way to cut it after a certain number of words, that or cut it after X# of characters and then round up if its in the middle of a word.

Basically I am trying to echo the first couple sentences of an article onto the homepage as a teaser.

Thanks
__________________
DVD Movie Release Database:
Please login or register to view this content. Registration is FREE
Truly is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 08-24-2010, 04:58 AM Re: Limit number of words pulled from DB
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Like this, you mean?
Code:
create table cut (strTxt text);

insert into cut values ('Hey all,I have come up with some pretty outlandish solutions to this problem, but I feel like there has to be something elegant and simple out there. I can easily cut a string after X# of characters, but whats the best way to cut it after a certain number of words, that or cut it after X# of characters and then round up if its in the middle of a word.
');

SELECT LEFT(strtxt, locate(' ', strtxt,
locate(' ', strtxt,
locate(' ', strtxt) + 1) + 1) - 1)
from cut;

drop table cut;
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 08-30-2010, 05:25 PM Re: Limit number of words pulled from DB
Truly's Avatar
Ultra Talker

Posts: 322
Trades: 0
I have been doing some more digging and this looks like the best solution:

SELECT SUBSTRING_INDEX(field, ' ', 25) FROM tablename

field: the column in the database
' ': the character that it is looking for (in this case its a blank space since I want to limit the number of words)
25: the number of times it sees ' ' before cutting it off
__________________
DVD Movie Release Database:
Please login or register to view this content. Registration is FREE
Truly is offline
Reply With Quote
View Public Profile
 
Old 08-30-2010, 08:30 PM Re: Limit number of words pulled from DB
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Sometimes you overlook the simpliest solutions.
At least I did.
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Reply     « Reply to Limit number of words pulled from DB
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.40914 seconds with 12 queries