在最后一个斜杠和问号之间提取文件名 [英] extract filename between last slash and question mark

查看:53
本文介绍了在最后一个斜杠和问号之间提取文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 regex 在最后一个斜杠和问号之间提取文件名我看了一些相关的答案

I want to extract filename between last slash and question mark using regex I read some related answers

([^/]*$)

但是我有几个域名,所以我想提取特定名称的文件名并寻找适用于所有域的正则表达式.我如何将其限制在某些域中?我的目标是替换域名,

But i have several domain names so i want to extract filename of specific names and looking for a regex that work for all domains. How can i limit it to certain domains? My target is to replace the domain name,

http://old.domain.com/asda/dsdasd/fsdfd/bvc/filename.mp4?fdgfsdgfgfsgf
http://new.domain.com/filename.mp4

真诚的

推荐答案

你可以试试

preg_replace('/(.*:\/\/).*\/(.*?)(\?.*|$)/', '$1new.domain.com/$2', $url );

用新的替换完整的域和路径.

replacing the full domain and path with the new one.

在 Ideone 上查看.

或者,如果您真的需要提取文件名(如您在问题中所说),请尝试

Or if you really need to extract the filename (as you say in the question) try

preg_match('/[^\/]+?(?=\?|$)/', $url, $matches);

如图所示.

这篇关于在最后一个斜杠和问号之间提取文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆