MyBB Community Forums

Full Version: Check something in message with REGEXP
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi, I'm trying to take images uploaded with imgur in messages from the forum X. That is to have a gallery (on index, portal or other pages) with all images uploaded from users from the forum X.

$query = $db->query("
		SELECT p.pid, p.message, p.fid, p.tid
		FROM ".TABLE_PREFIX."posts p
		WHERE p.visible='1' AND p.fid='105' AND LOWER(message) REGEXP '\\[img\\]http://i\.imgur\.com.*\\[/img\\]'
		ORDER BY p.pid DESC
		LIMIT 0, 10
	");
	while($thread = $db->fetch_array($query))
	{
		$testhatt_username = var_dump($query);
		$testhatt_postlink = get_thread_link(intval($testhatt['tid']));
		eval("\$testhatt_gallery_gallery .= \"".$templates->get("testhatt_gallery_gallery")."\";");
	}
		eval("\$testhatt_gallery = \"".$templates->get("testhatt_gallery")."\";");


And in template recall with <img src="{$testhatt_username}"> but thumbs don't appear Sad

How can I do?

Thank you in advance
Its really better to use regex before the post gets inserted into the database because using the keyword LOWER and the function REGEXP is going to kill your performance.

I'm not sure where you're getting {$fishhatt_username} from but it certainly isn't defined in the code you have posted. With the query you have, the array keys of $thread will be pid, message, fid, and tid.
Ops sorry, I was confusing with other plugin, the variable right is obviously {$testhatt_username}

How can I use regex before the post gets inserted into the database?

Thank you very much
You'd hook to either datahandler_post_insert_post or datahandler_post_insert_post_end. You will use the function preg_replace($pattern, $replacement, $string).
Thank you, so I should use:

$plugins->add_hook("datahandler_post_insert_post");

before of my query and then preg_replace.

I'm reading about this preg_replace, but I understand only it replaces a thing, but I don't want replace, only I want a "result". Then with that result I can add img src etc in template, no problem for that. How can I take image url of imgur for example?

thanks
I don't think dragonexperrt understood your request - if you want to query only posts which match the regex, you can simply use LIKE in the query:
AND LOWER(message) LIKE '%[img]http://i.imgur.com%[/img]%'
But then not sure what you want to achieve with var_dump() and especially why you're assigning it to a variable. It doesn't even return any value.. If you want to display images from posts, you need to extract them from $thread['message']. Definitely not a one-liner since:
a) there may be multiple matches in any post, not sure how you want to handle that
b) you need to capture the url inside the [img] tags - for example preg_match() can be used
There is an API to retrieve the data from imgur and a php class on GitHub but niere8 was unable to get it to work.

She asked me about this and I said that really, before styling the gallery she should actually get the data out and suggested she use a var_dump to see the results of her query as a start point. Not the final product....

Given that she has a big board then I personally think it would be insane to run a regexp on the posts table. Even using a like in that manner as suggested by Destroy666 is going to be a very hefty query.

I think she needs to get a none anonymous key and then echo out the gallery from imgur's API or start capturing the data at the point of entry; ie when the post is posted.
If I use imgur's API how can I have only images uploaded with my forum? I'm using imgur tutorial of martec to upload images from my forum, for this I thought it was better to look directly from post of Mybb. I don't want also all other images uploaded on imgur from other anonymous users or websites. Or I think it works in this way.

Then there are also limit of Imgur:  " Each application can allow approximately 1,250 uploads per day or approximately 12,500 requests per day." Requests are views / day?

Bof I don't know Sad

This works, but I have only link to image from a my album on imgur..and it's in alert -_- I would like image..

(function($){
var albumID = 'my albumID';
var albumAPI = "https://api.imgur.com/3/album/" + albumID + "/images";

$.ajax({
  url: albumAPI,
  headers:{
      'Authorization':'Client-ID xxxxxxxxxxxxx'
  },
  type: 'GET',
  dataType: 'json',
  success: function(data) { 

      alert(data.data[0].link);

  },
  error: function() { console.log("ERRORZ"); }
});

})(jQuery);

I tried with echo '<img src="'data'">'; etc, but nothing..