php 中的组替换

group replacement in php

我需要以这种方式修改 links(全局)

http://www.domain.de/specific-folder/{数据}/#link

对此:

http://{data}.domain.de/#link

{data} = changing folder name

我试过了,但我不知道如何在 php (preg_replace)?

中处理组替换
$re = "/\/specific-folder\/*([0-9,a-z,A-Z,-]*)(.*)/"; 
$str = "http://www.domain.de/specific-folder/data/#link"; 
$subst =  // works, but i can't get rid of the original search string (?)

非常感谢任何提示!

您可以使用:

$str = preg_replace('~(http://)www\.(domain\.de)/specific-folder/([^/]+)~', '', $str);
^[^.]*\.([^\/]*)\/.*?\/({.*?})\/(#.*)$

尝试 this.See 演示。

https://regex101.com/r/sH8aR8/44

$re = "/^[^.]*\.([^\/]*)\/.*?\/({.*?})\/(#.*)$/m";
$str = "http://www.domain.de/specific-folder/{data}/#link";
$subst = "http://./";

$result = preg_replace($re, $subst, $str);