正则表达式:修改img src标签 [英] Regex: Modify img src tags

查看:130
本文介绍了正则表达式:修改img src标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试用内嵌图像(data:image)替换html文档中的所有图像。
我有一个示例代码不起作用:

pre $函数data_uri($ filename){
$ mime = mime_content_type($ filename);
$ data = base64_encode(file_get_contents($ filename));
返回data:$ mime; base64,$ data;
}

函数img_handler($ matches){
$ image_element = $ matches [1];
$ pattern ='/(src = [\'])([^\'] +)([\'])/';
$ image_element = preg_replace($ ($匹配[3]),
$ image_element);
返回$ image_element;
}

$ content =(许多)不同的img标签
$ search ='(< img\s + [^> +>)'' ;
$ content = preg_replace_callback($ search,'img_handler',$ content);

有人可以检查这段代码吗?谢谢!
$ b $ p $ UPDATE:
警告file_get_contents()[function.file- get-contents]:文件名不能为空(...)

这意味着src url不在处理程序中:(



UPDATE 2

 <?php 
function data_uri $ filename
$ mime = mime_content_type($ filename);
$ data = base64_encode(file_get_contents($ filename));

returndata:$ mime; base64, $ data;
}

函数img_handler($ matches){
$ image_element = $ matches [0];
$ pattern ='/(src = [\'])([^\'] +)([\'])/';
$ image_element = preg_replace_callback($模式,create_function(
$ matchess,
$ matchess [1]。data_uri($ matchess [2])。$ matchess [3]),
$ image_element);
return $ image_element;
}

$ content ='< a href =http://upload.wikimedia.org/wikipedia/commons/thumb/4/44/Googlelogoi.png/ 180px-Googlelogoi.png>< img class =alignnonesrc =http://upload.wikimedia.org/wikipedia/commons/thumb/4/44/Googlelogoi.png/180px-Googlelogoi.pngalt =googlewidth =580height =326title =google/>< / a>';
$ search ='(< img\s + [^>] + >)';
$ content = preg_replace_callback($ search,'img_handler',$ content);

echo $ content;
?>

我已经上传这个测试文件 - > http://goo.gl/vWl9B

解决方案

你的正则表达式没问题。 e使用 create_function() 错误。随后内部 preg_replace_callback() 不起作用。对 data_uri()的调用发生在任何正则表达式替换发生之前,因此为什么未定义的文件名错误。



使用一个合适的回调函数:

  $ image_element = preg_replace_callback($ pattern,data_uri_callback,$ image_element); 

然后将代码移到那里:

 函数data_uri_callback($ matchess){
return $ matchess [1]。 data_uri($ matchess [2])。 $ matchess [3];
}


I try to replace all images in a html document with inline image (data:image). I've a sample code which does not work:

function data_uri($filename) {
$mime = mime_content_type($filename);
$data = base64_encode(file_get_contents($filename));
return "data:$mime;base64,$data";
}

function img_handler($matches) { 
$image_element = $matches[1];    
$pattern = '/(src=["\'])([^"\']+)(["\'])/'; 
$image_element = preg_replace($pattern, create_function( 
$matches, 
$matches[1] . data_uri($matches[2]) . $matches[3]), 
$image_element);     
return $image_element;
}

$content = (many) different img tags
$search = '(<img\s+[^>]+>)';
$content = preg_replace_callback($search, 'img_handler', $content);

Could somebody check this code? Thanks!

UPDATE: (...) Warning file_get_contents() [function.file-get-contents]: Filename cannot be empty (...)

That means the src url is not in the handler :(

UPDATE 2

<?php
function data_uri($filename) {
    $mime = mime_content_type($filename);
    $data = base64_encode(file_get_contents($filename));

    return "data:$mime;base64,$data";
}

function img_handler($matches) { 
$image_element = $matches[0];
$pattern = '/(src=["\'])([^"\']+)(["\'])/'; 
$image_element = preg_replace_callback($pattern, create_function( 
$matchess,
$matchess[1] . data_uri($matchess[2]) . $matchess[3]), 
$image_element); 
return $image_element;
}

$content = '<a href="http://upload.wikimedia.org/wikipedia/commons/thumb/4/44/Googlelogoi.png/180px-Googlelogoi.png"><img class="alignnone" src="http://upload.wikimedia.org/wikipedia/commons/thumb/4/44/Googlelogoi.png/180px-Googlelogoi.png" alt="google" width="580" height="326" title="google" /></a>';
$search = '(<img\s+[^>]+>)';
$content = preg_replace_callback($search, 'img_handler', $content);

echo $content;
?>

I've upload this test file -> http://goo.gl/vWl9B

解决方案

Your regex is alright. You are using create_function() wrong. And subsequently the inner preg_replace_callback() doesn't work. The call to data_uri() happens before any regex-replacement takes place, hencewhy the undefined filename error.

Use a proper callback function:

$image_element = preg_replace_callback($pattern, "data_uri_callback", $image_element);

Then move your code into there:

function data_uri_callback($matchess) {
    return  $matchess[1] . data_uri($matchess[2]) . $matchess[3];
}

这篇关于正则表达式:修改img src标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆