This is my url:
这是我的网址:
ftp://dynamic_text:user_password@my-ftp-domain.com/so-on/param
My regex will turn it into this:
我的正则表达式会把它变成这样:
ftp://*****:*****@my-ftp-domain.com/so-on/param
Note that the url can start with either ftp
or http
.
请注意,url可以以ftp或http开头。
regex:
My regex below will always return ftp
regardless if my url started with http
.
无论我的网址是否以http开头,我的正则表达式都会返回ftp。
preg_replace('@(ftp|http)://(.*:.*)\@@', 'ftp://****:****@', $url);
Now my question is: how can I modify my code, so that it will dynamically return ftp
or http
depending on how my url started.
现在我的问题是:如何修改我的代码,以便根据我的url的启动方式动态返回ftp或http。
I read about Named Groups, but I wasn't able to solve it.
我读到了有关命名组的内容,但我无法解决它。
1 个解决方案
#1
Just change the ftp
part in your replacement to $1
to get the value of the first group, e.g.
只需将替换中的ftp部分更改为$ 1即可获得第一组的值,例如
preg_replace('@(ftp|http)://(.*:.*)\@@', '$1://****:****@', $url);
//^^
#1
Just change the ftp
part in your replacement to $1
to get the value of the first group, e.g.
只需将替换中的ftp部分更改为$ 1即可获得第一组的值,例如
preg_replace('@(ftp|http)://(.*:.*)\@@', '$1://****:****@', $url);
//^^