开发者

PHP regex to include everything before and after a domain name

开发者 https://www.devze.com 2023-02-03 23:27 出处:网络
Say I have a referrer that is http://dwz07.foo.net/Player.aspx?All&PID=4321&UID=359010016&STime=921596&SToken=5c93210bdb17ab2c930a1e365b8b05a8e6aca013

Say I have a referrer that is http://dwz07.foo.net/Player.aspx?All&PID=4321&UID=359010016&STime=921596&SToken=5c93210bdb17ab2c930a1e365b8b05a8e6aca013

What would be the proper regex to include everything before and after foo.net? Like, I want to include every subdomain and everythi开发者_Python百科ng after foo.net/

The best I can come up with:

^([^.]+).foo.net$

But that doesn't work.

Thank you.


Try this:

$string = 'http://dwz07.foo.net/Player.aspx?All&PID=4321&UID=359010016&STime=921596&SToken=5c93210bdb17ab2c930a1e365b8b05a8e6aca013';
$pattern = '/^(.*)(?:\.foo.net.*)/';
$replacement = '$1';
echo preg_replace($pattern, $replacement, $string);


$ anchors the regex to the end of the string, so it would only match a URL which ended with .net.

Your URL doesn't finish with .net, it has a / after: just remove the $ to fix that.

0

精彩评论

暂无评论...
验证码 换一张
取 消