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
adster
Forum Newbie
Posts: 5 Joined: Mon Jun 06, 2005 11:11 pm
Post
by adster » Mon Jun 06, 2005 11:13 pm
Hi Guys,
I have a blob in my db and i would like to be able to display say the first 50 words from the blob.
Displaying the entire blob is no problem, i just can't seem to figure out how to display a summary of it.
any help would be greatly appreciated.
cheers,
Adam
artexercise
Forum Commoner
Posts: 33 Joined: Thu Nov 20, 2003 9:38 am
Location: Raleigh, NC
Post
by artexercise » Tue Jun 07, 2005 9:01 am
one method would be to explode the blob into an array using the " " space as a delimiter. Then displaying the first 50 elements of the array.
Another method would probably involve regular expressions which I am far from an expert on.
John Cartwright
Site Admin
Posts: 11470 Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:
Post
by John Cartwright » Tue Jun 07, 2005 9:24 am
Code: Select all
preg_match('#^\s*(.{60,}?)\s+.*$#s',$row['data'],$matches);
I think you should grab the first # of characters instead of words so you can more easily control the total length. This snipplet will grab the first 60 characters, and not cut off any words.
I'm not a guru in mysql, but you could try doing this
Code: Select all
$sql = "e;SELECT SUBSTRING(0,60,`columname`) FROM `data`"e;;
Not sure if that would work, and it also will cut off words. I recommend using regex.
Last edited by
John Cartwright on Tue Jun 07, 2005 2:56 pm, edited 1 time in total.
adster
Forum Newbie
Posts: 5 Joined: Mon Jun 06, 2005 11:11 pm
Post
by adster » Tue Jun 07, 2005 12:38 pm
Hi JCart,
Thanks for the reply.
When i use your php code snippet
$comment = preg_match('#^\s*(.{100,}?)\s+.*$#s',$row['comment']);
I get ´1´ as my output when i echo my variable $comment.
any ideas on what i might be doing wrong?
cheers,
Adam
Burrito
Spockulator
Posts: 4715 Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah
Post
by Burrito » Tue Jun 07, 2005 1:28 pm
try:
Code: Select all
preg_match('#(^\s*(.{60,}?)\s+.*$)#s',$row['data'],$matches);
echo $matches[1];
edit: err maybe matches[2];
adster
Forum Newbie
Posts: 5 Joined: Mon Jun 06, 2005 11:11 pm
Post
by adster » Tue Jun 07, 2005 8:51 pm
Burrito,
Thanks very much. All solved, now i think it's time to learn all about regex. argh
thanks to all for the help, very much appreciated. Now i can complete my i hate russell crowe page
cheers,
Adam
Skara
Forum Regular
Posts: 703 Joined: Sat Mar 12, 2005 7:13 pm
Location: US
Post
by Skara » Tue Jun 07, 2005 9:35 pm
Now i can complete my i hate russell crowe page
well thank god for that!
Burrito
Spockulator
Posts: 4715 Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah
Post
by Burrito » Tue Jun 07, 2005 10:00 pm
adster wrote:
Thanks very much. All solved, now i think it's time to learn all about regex. argh
d11 wrote a fantastic tutorial on regex
here