Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP正则表达式替换图像SRC属性_Php_Html_Regex_Src_Preg Replace Callback - Fatal编程技术网

PHP正则表达式替换图像SRC属性

PHP正则表达式替换图像SRC属性,php,html,regex,src,preg-replace-callback,Php,Html,Regex,Src,Preg Replace Callback,我试图找到一个正则表达式,它允许我替换图像中的SRC属性。以下是我所拥有的: function getURL($matches) { global $rootURL; return $rootURL . "?type=image&URL=" . base64_encode($matches['1']); } $contents = preg_replace_callback("/<img[^>]*src *= *[\"']?([^\"']*)/i", getURL,

我试图找到一个正则表达式,它允许我替换图像中的
SRC
属性。以下是我所拥有的:

function getURL($matches) {
  global $rootURL;
  return $rootURL . "?type=image&URL=" . base64_encode($matches['1']);
}

$contents = preg_replace_callback("/<img[^>]*src *= *[\"']?([^\"']*)/i", getURL, $contents);
函数getURL($matches){
全球$rootURL;
返回$rootURL。“?type=image&URL=“.base64_encode($matches['1']);
}
$contents=preg\u replace\u回调(“/
在大多数情况下,这很有效,除了当$contents被回送到屏幕时,
src=“
属性之前的任何内容都被删除。最后,
src
被正确更新,更新后的图像URL之后的所有属性都返回到屏幕

我对使用DOM或XML解析库不感兴趣,因为这是一个非常小的应用程序

如何修复正则表达式,以便只更新
SRC
的值


谢谢您的时间!

是否进行另一个分组并将其添加到返回值之前

function getURL($matches) {
  global $rootURL;
  return $matches[1] . $rootURL . "?type=image&URL=" . base64_encode($matches['2']);
}

$contents = preg_replace_callback("/(<img[^>]*src *= *[\"']?)([^\"']*)/i", getURL, $contents);
函数getURL($matches){
全球$rootURL;
返回$matches[1]。$rootURL。“?type=image&URL=“.base64_encode($matches['2']);
}
$contents=preg_replace_回调(“/(使用一个而不是贪婪的回调)

这可能是您的问题:

/<img[^>]*src *= *[\"']?([^\"']*)/
         ^
/
将其更改为:

/<img[^>]*?src *= *[\"']?([^\"']*)/
/
通过这种方式,
[^>]*
匹配括号表达式中可能的最小值,而不是可能的最大值