Regexp to extract content of [img]...[/img] tag
#1
Almost finished with my topposts plugin, but I need to find out the regexp to bring the content of the first img tag.

With my poor regexp knowledge I came up with this:
Quote:\[img\](.*?)\[/img\]

but it didnt work, dont know why Smile

I would really appreciate any help,

Eco

Done, used this:

Quote://try to locate the first image on the post text
if (preg_match("/\[img\](.*?)\[\/img\]/i", $message, $matches)) {
return $matches[0];
}
Reply
#2
Just wanted to post what I use to match all [img] tags in a post:

#(?P<wholestring>\[img\](\r\n?|\n?)(?P<url>https?://([^<>\"']+?))\[/img\])#ise

For example:

    preg_match_all("#(?P<wholestring>\[img\](\r\n?|\n?)(?P<url>https?://([^<>\"']+?))\[/img\])#ise", $message, $matches);

    // No match? Let's skip this loop around
    if (empty($matches) OR !is_array($matches) OR !isset($matches['wholestring']) OR !isset($matches['url'])) {
        continue;
    }

    $i = 0;
    foreach ($matches['url'] as $match) {

Just in case anybody else stumbles upon this and wants something similar. The URL to the image is stored in $matches['url'] and the whole string (including [img] tags) is stored in $matches['wholestring'].
Reply
#3
This user has been denied support. This user has been denied support.
See how MyBB matches it then do something equivalent. Otherwise you'll have img tags matched by MyBB but not by your own code; or vice versa.

MyBB uses four separate preg_replace calls for this thing (for align and dimensions and combinations thereof). It can easily be merged into one. Exercise left for the reader.
Reply
#4
Worked, thanks a lot. What doesn't work now is the local download of images.

I have this function at the moment:

Quote:function downloadFile ($url, $path) {

$newfname = $path;
$file = fopen ($url, "rb");
if ($file) {
$newf = fopen ($newfname, "wb");

if ($newf)
while(!feof($file)) {
fwrite($newf, fread($file, 1024 * 8 ), 1024 * 8 );
}
}

if ($file) {
fclose($file);
}

if ($newf) {
fclose($newf);
}
}

But I get timeout error inside the fopen.

Quote:Fatal error: Maximum execution time of 30 seconds exceeded in E:\wamp\www\mybb\inc\plugins\topposts.php on line 971

Does anyone have a dowload function which is robust and wont make all mybb fail?
Reply
#5
I am using this routine for downloading, but although it works great locally at the server it hangs all clients. I'm afraid I am having some concurrency issue. I'm testing this at 4 in the morning, whem I have very few (1-6) users, and the plugin is run only on the index page, which have around 0-1 users cuncurrently at this time in the night.

Would someone be gentle to test this for me?

Think better about it, it doesn't seem like a concurrency error, as it doesn't even generate any of the thumb nails.
Reply
#6
Ideally, this kind of thing should be ran as a task or by cron. You don't want to run the same script for everybody logged on. Have a look at this script that I wrote to be ran via cron: https://github.com/euantor/MyBB-Local-Images
Reply
#7
Thanks Euan T, I will try to use this at the next version, once I can get this to work.

I posted the code on the thread below, if you could have a look, and test if possible, it would be awesome. The result is really good, I guarantee Smile

http://community.mybb.com/thread-139455-...04186.html

Interesting the "checkImageFile" function, that might help to make the code more robust Smile
Reply
#8
I'll take a look today Smile

checkImageFile() isn't quite 100%. It still needs improving a bit Smile
Reply
#9
(05-17-2013, 09:03 AM)Euan T Wrote: I'll take a look today Smile

checkImageFile() isn't quite 100%. It still needs improving a bit Smile


Thank you!! I did some refactoring and clean up, but the problem persists. I added a picture of the plugin at the plugin thread.

http://community.mybb.com/thread-139455.html

It works locally but hangs at the server, I'm really stuck :/

(05-17-2013, 09:03 AM)Euan T Wrote: checkImageFile() isn't quite 100%. It still needs improving a bit Smile

Actually I ended up not using because if was giving some errors :/
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)