简体   繁体   English

preg_replace图片src

[英]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() : 我试图浏览我的内容并将图像源标签替换为其他内容(尤其是在受支持时为dataURIs)-基于我在这里已阅读的几个问题,我正在尝试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: wpdu_base64_encode_image($upload_dir['path'].'/'.\\1)的问题是wpdu_base64_encode_image($upload_dir['path'].'/'.\\1) ,它基本上是输出preg_replace结果-当前正在获取:

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: $upload_dir['path']正确地输出了我需要的图像文件夹的路径,但是还有一些检查是我已经尝试过但尚未实现的:

  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() ?) 检查图像源是否是相对的,如果是相对的,则剥离域(当前可以使用site_url() ,我假设它必须是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 如果图像甚至不在服务器本地(再次-我假设使用site_url()检查),请跳过该图像

I'm not as familiar with preg_replace() if anyone has suggestions, I'd really appreciate it. 如果有人提出建议,我对preg_replace()并不熟悉,我真的很感激。 Thanks! 谢谢!

Edit: Should I use http://simplehtmldom.sourceforge.net/ instead? 编辑:我应该改用http://simplehtmldom.sourceforge.net/吗? 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. 通常,用正则表达式解析HTML不是一个好主意,您绝对应该考虑使用其他东西,例如适当的HTML解析器。 You don't quite need simplehtmldom, the built-in DOMDocument and getElementsByTagName will do the job nicely. 您并不需要simplehtmldom,内置的DOMDocumentgetElementsByTagName可以很好地完成此工作。

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 : 为了解决当前的问题,可以使用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. 您当前的代码尝试在完全不相关的上下文中使用占位符\\1 ,这就是为什么会出现解析错误的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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