Pulling first image from thread

Naz

XenForo developer
Staff member
Working on a little project and came across a bit of a hurdle.

I was wondering if anyone had any ideas how I could go about programatically pulling the first image in a thread. So for instance, pulling the contents of the first occurrence of the IMG BBCode.

Thanks.
 
1. Get the thread with the first post
2. Parse the first post or regex to find the IMG BBCode
3. Do whatever you want to do with it!

PS: I actually dealt with a pretty similar problem recently. I just checked if the thread's first post had an attachment and I fetched the first attachment which was of image type.
 
1. Get the thread with the first post
2. Parse the first post or regex to find the IMG BBCode
3. Do whatever you want to do with it!

PS: I actually dealt with a pretty similar problem recently. I just checked if the thread's first post had an attachment and I fetched the first attachment which was of image type.
I might try attachments as it sounds a lot easier to achieve what I want but then using IMG BBCode, I'll probably learn a few new things too, haha.

Do you happen to know a good place where I can read up more on regular expressions? I'm pretty basic at the moment.
 
Regex are always tough! :P

Some good resources that I used personally to get over the fear of Regex are:
1. Tutsplus tutorials. I don't remember exactly but I think they were by Jeffery way, have a look around that site and you might just find it. Think those tuts could date back 2-4 years from now.
2. regex101.com -> Great tool for testing out regular expressions. It's much better than doing it in PHP and trying to to see if it works or not.

Besides that all you need to be pro at regular expression is a lot of practice. Once you get a hang of it, you will always think why hadn't you figured them out before ;)
 
  • Like
Reactions: Naz
Back
Top Bottom