I'm using this line to change all images in my $comments
string:
$comments = preg_replace("{<img\\s*(.*?)src=('.*?'|\".*?\"|[^\\s]+)(.*?)\\s*/?>}ims", '<a class ="gallery"rel="'.$pagelink.'"href=$2><img $1src=$2 $3 name= $2 $3 /></a>', $comments);
This works like a charm, it wraps it in <a>
and adds some stuff. Now I need to alter the src path.
I want to add "mcith/mcith_"
to every image in a string. i had a look at dom functions, but that didn't really seem to do the trick.
An image path looks like this: "/uploads/userdirs/admin(variable dir changes from user to user)/image.jpeg"
I want it to be this: "/uploads/userdirs/admin/mcith/mcith_image.jpeg"
On the similar question I got the answer using pathinfo, so I tried doing this:
if (preg_match('/<img.+src=[\'"](?P<src>.+?)[\'"].*>/i', $comments, $image) ) {
$imagedir = $image['src'];
$pathchange = pathinfo($imagedir);
$comments = $pathchange['dirname'] . '/mcith/mcith_' . $pathchange['basename'];
}
But that doesn't work as I want it to as it messes up my entire string instead, text and all. I think the solution is in the preg_replace line, but can't figure out how to add stuff to the src on that line.