Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-22-2004, 06:25 PM   PM User | #1
DoubleV
Regular Coder

 
Join Date: Dec 2002
Location: the windy city
Posts: 140
Thanks: 0
Thanked 0 Times in 0 Posts
DoubleV is an unknown quantity at this point
displaying first x characters, but not cutting in the middle of the word

I want to display the forst x characters of a db text field, which I guess I can do using a substring. But I don't want it to cut off in the middle of the word. How would I accomplish that?
DoubleV is offline   Reply With Quote
Old 04-22-2004, 06:35 PM   PM User | #2
bcarl314
Mega-ultimate member


 
Join Date: Jun 2002
Location: Winona, MN - The land of 10,000 lakes
Posts: 1,855
Thanks: 1
Thanked 45 Times in 42 Posts
bcarl314 will become famous soon enough
Hmm, I know I've seen this here before.

How about something like...

PHP Code:
$text preg_match_all("/(.{1,50}\s.{1,15})?/",$dataBaseText,$matches);
print 
$matches[0]; 

Not tested, but something similar should work.
bcarl314 is offline   Reply With Quote
Old 04-22-2004, 06:41 PM   PM User | #3
raf
Master Coder


 
Join Date: Jul 2002
Posts: 6,589
Thanks: 0
Thanked 0 Times in 0 Posts
raf will become famous soon enoughraf will become famous soon enough
Yes. Has been posted here before. I use this

PHP Code:
/*jesus at home dot com
03-Apr-2003 08:53 
How to make a nice trim of text without break words in the middle :*/

 
$maxTextLenght=125;
  
$aspace=" ";
  if(
strlen($text) > $maxTextLenght ) {
     
$text substr(trim($text),0,$maxTextLenght); 
     
$text substr($text,0,strlen($text)-strpos(strrev($text),$aspace));
     
$text $text.'...';
  } 
So you cut of, the run a reverse search for a space and do a substr with that is number of characters.

Of course, when i pull it from a db, i do the initial chopping of with sql
PHP Code:
sql="select left(variable, 125)  as variable from table";
...
$aspace=" ";
if(
strlen($text) > $maxTextLenght ) {
     
$text substr($text,0,strlen($text)-strpos(strrev($text),$aspace));
     
$text $text.'...';

__________________
Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html
raf is offline   Reply With Quote
Old 04-22-2004, 10:03 PM   PM User | #4
DoubleV
Regular Coder

 
Join Date: Dec 2002
Location: the windy city
Posts: 140
Thanks: 0
Thanked 0 Times in 0 Posts
DoubleV is an unknown quantity at this point
Quote:
Originally Posted by raf
Yes. Has been posted here before.
I did a search, so I saw that. But i got confused, so figured I'd ask again.

Quote:
Originally Posted by raf
PHP Code:
sql="select left(variable, 125)  as variable from table"
can you please explain what "variable" should stand for in this line?
DoubleV is offline   Reply With Quote
Old 04-22-2004, 11:01 PM   PM User | #5
raf
Master Coder


 
Join Date: Jul 2002
Posts: 6,589
Thanks: 0
Thanked 0 Times in 0 Posts
raf will become famous soon enoughraf will become famous soon enough
inside the left-function, this needs bto be the name of the column /control /variable inside your table. The column you wan't to pulle the value from. The second variable is then an alias (could be the same name as the original columname. I suppose you'll kknow that 'table' needs to be replaced by the tablename.
__________________
Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html
raf is offline   Reply With Quote
Old 04-23-2004, 03:11 AM   PM User | #6
misterx
Regular Coder

 
Join Date: Dec 2002
Location: Seattle, WA
Posts: 116
Thanks: 1
Thanked 0 Times in 0 Posts
misterx is an unknown quantity at this point
Don't forget about the awesome concatenation operator! Kind of a silly revision but you could modify the last line like this:

from:
PHP Code:
$text $text.'...'
to:
PHP Code:
$text .= '...'
Makes no difference....I just like small code so I try to squeez out extra stuff wherever I can.
misterx is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

Forum Jump


All times are GMT +1. The time now is 07:16 AM.


Advertisement
Log in to turn off these ads.