如何在PHP中使用反向引用

如何在PHP中使用反向引用,php,preg-replace,backreference,Php,Preg Replace,Backreference,我想使用preg_replace()在文本体中找到的每个文件扩展名的末尾添加一个字符 以下是一些示例文本: $string='http://www.mysite.com/expert/images/imageone.jpghttp://www.mysite.com/expert/images/imagetwo.jpg'; 此搜索和替换在TextWrangler中运行良好,在文件扩展名后添加分号: (\.(jpg|gif|html?|php|tiff?|pdf|png)) \1; 翻译成P

我想使用preg_replace()在文本体中找到的每个文件扩展名的末尾添加一个字符

以下是一些示例文本:

$string='http://www.mysite.com/expert/images/imageone.jpghttp://www.mysite.com/expert/images/imagetwo.jpg';
此搜索和替换在TextWrangler中运行良好,在文件扩展名后添加分号:

(\.(jpg|gif|html?|php|tiff?|pdf|png))


\1;
翻译成PHP,但是不起作用,没有效果;没有错误

preg_replace("/(\.(jpg|gif|html|php|tif|tiff|pdf|htm|png))/","\\1;",$string);

它对我来说非常有效,但您应该尝试使用
$1

$string = preg_replace("/.../","$1;",$string);
或者将替换项用单引号引起来:

$string = preg_replace("/.../",'\\1;',$string);

@斯林基:那么错误一定在别的地方。确保您有
错误报告(E\u ALL)
。如果不这样做,您还必须将值赋回
$string
。是的,我没有将结果赋回变量。谢谢