1

我想使用 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);
4

1 回答 1

3

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

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

或将替换放在单引号中:

$string = preg_replace("/.../",'\\1;',$string);
于 2011-01-14T17:51:00.773 回答