Regex preg_替换以捕获内部图像标签

Regex preg_替换以捕获内部图像标签,regex,html-parsing,preg-replace,Regex,Html Parsing,Preg Replace,你好,我希望有人能帮我解决这个怀孕问题 我有以下preg\u replace/\/[^\/]*.jpg/,.jpg,$input\u行 这个想法是将我的wordpress站点中的图片替换为它们的原始名称。。。该文件已存在于非缓存文件夹中,因此我不需要担心该部分 <img src="http://url.com/wp-content/uploads/cache/2014/02/flag/278439615.jpg"> 现在,下一步是从URL中删除number.jpg。该数字可以是字母

你好,我希望有人能帮我解决这个怀孕问题

我有以下preg\u replace/\/[^\/]*.jpg/,.jpg,$input\u行

这个想法是将我的wordpress站点中的图片替换为它们的原始名称。。。该文件已存在于非缓存文件夹中,因此我不需要担心该部分

<img src="http://url.com/wp-content/uploads/cache/2014/02/flag/278439615.jpg">
现在,下一步是从URL中删除number.jpg。该数字可以是字母或数字

所有URL中的常量是/和.jpg的最后一次出现,preg_替换了/\/[^\/]*.jpg/,.jpg,$input_行;照顾

然而,问题是preg_replace需要查找图像标记中的标记,而不是替换图像标记中不存在的任何代码

我如何添加到regex preg_replace中,以便只查看替换的图像标记内部

理想的最终结果是一个如下所示的URL:

<img src="http://url.com/wp-content/uploads/2014/02/flag/278439615.jpg">
<img src="http://url.com/wp-content/uploads/2014/02/flag.jpg">
regex preg_replace还需要替换匹配的所有事件,因为页面上可能有多个图像


非常感谢您的帮助

您可以使用此替换:

$pattern = <<<'EOD'
~
<img \s
(?> [^>s]++ | \Bs | s(?!rc\s*=) )* # possible content before the src attribute 
src \s* = \s* ["']? [^"\'\s>]+?    # start of the src attribute (until /cache/)
\K                                 # reset all from match result
/cache(?=/)
([^\s"'>]*?)                       # capture the path before the filename
/[^\s/]+ (?=\.jpg [>"'\s])         # the filename followed by the .jpg extension
~ix
EOD;
$result = preg_replace($pattern, '$1', $input_lines);