简体   繁体   中英

preg_replace image src

I'm attempting to scan through my content and replace image source tags with something else (more notably, dataURIs when supported) - based on a couple of questions I've read through here, I'm trying preg_replace() :

// Base64 Encodes an image
function wpdu_base64_encode_image($imagefile) {
    $imgtype = array('jpg', 'gif', 'png');
    $filename = file_exists($imagefile) ? htmlentities($imagefile) : die($imagefile.'Image file name does not exist');
    $filetype = pathinfo($filename, PATHINFO_EXTENSION);
    if (in_array($filetype, $imgtype)){
        $imgbinary = fread(fopen($filename, "r"), filesize($filename));
    } else {
        die ('Invalid image type, jpg, gif, and png is only allowed');
    }
    return 'data:image/' . $filetype . ';base64,' . base64_encode($imgbinary);
}

// Do the do
add_filter('the_content','wpdu_image_replace');
function wpdu_image_replace($content) {
    $upload_dir = wp_upload_dir();
    return preg_replace( '/<img.*src="(.*?)".*?>/', wpdu_base64_encode_image($upload_dir['path'].'/'.\1), $content );
}

The problem I'm running into is wpdu_base64_encode_image($upload_dir['path'].'/'.\\1) which is basically outputting the preg_replace result - currently getting:

Parse error: syntax error, unexpected T_LNUMBER, expecting T_STRING

$upload_dir['path'] is correctly outputting the path to the image folder that I need, but there's also a few checks that I've tried and haven't been able to achieve as of yet:

  1. Check if the image source is relative and if it is, strip off the domain (currently can be done with site_url() which I'm assuming would need to be of preg_replace() ?)
  2. If the image is not even local to the server (again - I'm assuming using the site_url() check), skip over it

I'm not as familiar with preg_replace() if anyone has suggestions, I'd really appreciate it. Thanks!

Edit: Should I use http://simplehtmldom.sourceforge.net/ instead? Seems like a pretty-heavy hammer, but if that's a more reliable way then I'm all for it - anyone use that before?

In general, parsing HTML with regular expressions is not a very good idea and you should definitely look into using something else such as a proper HTML parser. You don't quite need simplehtmldom, the built-in DOMDocument and getElementsByTagName will do the job nicely.

To get to your current problem, this type of transformation (where you want each replacement to be an arbitrary function of a match) is done using preg_replace_callback :

$path = $upload_dir['path']; // for brevity

return preg_replace_callback(
    '/<img.*src="(.*?)".*?>/',
    function ($matches) use($path) { 
        return wpdu_base64_encode_image($path.'/'.$matches[1]);
    },
    $content
);

Your current code attempts to use the placeholder \\1 in a completely unrelated context, which is why you get the parse error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM