preg_replace to link all images in content? (2 posts)

  1. eliotc
    Posted 8 years ago #

    Hello, I'm trying make a short plugin to replace all <img> tags in the_content with <href><img> tags. Is this possible with preg_replace?

    More specifically, i'd like to place every:
    <img src="anyfile">
    <a href="anyfile"><img src="anyfile"></a>

    I'm not experienced with regex, but here is what i have so far, which isn't working:

    define("IMAGE_FILETYPE", "(bmp|gif|jpeg|jpg|png)", true);
    function everyimage_create($string){
    	$pattern = '/<a(.*?)img(.*?)src="(.*?).(bmp|gif|jpeg|jpg|png)"(.*?)>/i';
      	$replacement = '<a$1$2href="$3.$4" rel=\'lightbox[imagegroup]\'$5><a img src=$3.$4"></a>';
    	return preg_replace($pattern, $replacement, $string);
    add_filter('the_content', 'everyimage_create');

    The overall goal is to have embedded images be clickable, so that they open in their original size using lightbox (or slimbox, etc.)

    Thanks for any help or tips.

  2. eliotc
    Posted 8 years ago #

    i ended up using a hacked up version of the Image Resizer plugin.

Topic Closed

This topic has been closed to new replies.

About this Topic