RegEx get id from Aeva Embed

nend

  • When is a theme, no longer what it was when installed?
  • Posts: 165
RegEx get id from Aeva Embed
« on August 2nd, 2012, 03:25 AM »
Hi,

I am wondering how to get an id from a Aeva BBC. I just need the id and nothing more. What I want to do is pull the id from the first BBC only in a post.

Here is what I have, it works prefect but it only does ids with 3 digits, if I switch the number to 2 then two digits.
Code: [Select]
if (preg_match('[smg id=([0-9]{3})(.+)]', $row['first_body'], $match)) {
$row['aeva_thumb'] = $scripturl . '?action=media;sa=media;in='.$match[1].';thumb';
}

RegEx has always thrown me off. I can usually get by trying to make an expression, but I am no expert in this area.

Arantor

  • As powerful as possible, as complex as necessary.
  • Posts: 14,278
Re: RegEx get id from Aeva Embed
« Reply #1, on August 2nd, 2012, 03:30 AM »
Try:

Code: [Select]
if (preg_match('[smg id=(\d+)(.*)]', $row['first_body'], $match)) {
$row['aeva_thumb'] = $scripturl . '?action=media;sa=media;in='.$match[1].';thumb';
}

\d is a shorthand for 0-9, and you want continuous digits, which may or may not be followed by anything after it before the bracket.


There's a really great program called Regex Coach which will let you apply a base text and a regex to it and step through how it will be processed.
When we unite against a common enemy that attacks our ethos, it nurtures group solidarity. Trolls are sensational, yes, but we keep everyone honest. | Game Memorial

nend

  • When is a theme, no longer what it was when installed?
  • Posts: 165
Re: RegEx get id from Aeva Embed
« Reply #2, on August 2nd, 2012, 03:39 AM »
I just tried a online one and got this, it seems to work.

Code: [Select]
if (preg_match('[smg id=([0-9]*)(.*)]', $row['first_body'], $match)) {
$row['aeva_thumb'] = $scripturl . '?action=media;sa=media;in='.$match[1].';thumb';
}

I think it is the same expression as yours just expressed differently I guess.
Re: RegEx get id from Aeva Embed
« Reply #3, on August 2nd, 2012, 03:45 AM »
Just tested fully and it works.

Now it checks to see if there is a Aeva Media item in the topic and change the post image to the Aeva thumbnail. If there is no Aeva Media item in the post then it changes the thumbnail to the board thumbnail. If there is no board thumbnail it looks to see if there is a parent board thumbnail. If none then it defaults to the default thumbnail.

It also changes the open graph thumbnail. Been putting allot into this site of mine lately.

Arantor

  • As powerful as possible, as complex as necessary.
  • Posts: 14,278
Re: RegEx get id from Aeva Embed
« Reply #4, on August 2nd, 2012, 03:46 AM »
Similar but not quite the same.

The * in the 0-9 part will also happily match [smg id=] in yours, the + is quite important since it matches 1 or more, as opposed to * for 0 or more.

Sounds like fun though :) The OG stuff is something I keep thinking about doing but never seem to get around to it - though I have a feeling it might not be appreciated too well as a core function, perhaps better as a plugin, don't know.

nend

  • When is a theme, no longer what it was when installed?
  • Posts: 165
Re: RegEx get id from Aeva Embed
« Reply #5, on August 2nd, 2012, 03:55 AM »
Ok, added your suggestion and still going.

You know I only had to modify two queries in the recent post, one for guest and one for members. The rest of the five queries already had the body loaded. That recent post query of mine is getting ugly, good thing I have it cached for quite a while.

Arantor

  • As powerful as possible, as complex as necessary.
  • Posts: 14,278

Nao

  • Dadman with a boy
  • Posts: 16,080
Re: RegEx get id from Aeva Embed
« Reply #7, on August 2nd, 2012, 09:33 AM »
Just, err...

Code: [Select]
if (preg_match('[(?:smg|media) id=(\d+)', $row['first_body'], $match)) {

- Added support for the Wedge version of the tag :P (Although I have plans to do 'post thumbnails' in Wedge by default anyway...)
- It's unlikely someone will type the beginning of a tag and then stop... So I just removed everything after the \d, it was useless.

Oh, how I miss the good old days when I was building Noisen.com... I was the only one who could see the code, and I had no need to add options/language strings in the admin area so I kept adding stuff everywhere...