problems re displaying blobs

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
adster
Forum Newbie
Posts: 5
Joined: Mon Jun 06, 2005 11:11 pm

problems re displaying blobs

Post by adster »

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
User avatar
artexercise
Forum Commoner
Posts: 33
Joined: Thu Nov 20, 2003 9:38 am
Location: Raleigh, NC

Post by artexercise »

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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

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 = &quote;SELECT SUBSTRING(0,60,`columname`) FROM `data`&quote;;
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 »

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
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

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 »

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 :D

cheers,
Adam
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Post by Skara »

Now i can complete my i hate russell crowe page
well thank god for that! :lol:
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

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