MyBB Community Forums

Full Version: Preg replace for img src ?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am failing at it. Trying since couple of minutes, thought I'd ask.

What I want to do/am doing is trying to change:

<img src="someimage.png" alt="someimage" />

to

<img src="someimage.png" class="myclassname" alt="someimage" />

But somehow the class isn't being replaced.

Any way to figure it out? Shy
Can you please paste the code you're working on ?
Well, its not much in detail , never tried this preg replace with image links. For a href (links) I know how to make it and it works fine.

Well, to explain in detail, I am running a hook on showthread_start, and trying to replace all the images tagged link (like <img src thing) and adding a class to them, say replacing:

Quote:<img src="someimage.png" alt="someimage" />

to

Quote:<img src="someimage.png" class="myclassname" alt="someimage" />

I use something like;

$replace = str_replace('<img src="', '<img class="myclassname" src="', 'alt=""');

Tried several other combinations as well, but no luck. Any ideas?
Hook into parse_message_end instead and do something like this:

function example_appendclasstoimg(&$message)
{
	$find = '<img src="';
	$replace = '<img class="example" src="';
	$message = str_replace($find, $replace, $message);

	return $message;
}
Thanks Fabio, that indeed did the work. Smile