donglingsai2880 2015-12-10 18:27
浏览 51
已采纳

我想从带有php的url检索没有扩展名的文件名

I have an url which contains various POST-DATA in it And an image file at last. My link is : http://website-link.com/?page=gf_signature&signature=565dbca63791e5.87676354.png

I want to seperate the 565dbca63791e5.87676354.png from the url and seperate the extension (.png) from it.

I can do it :but it is from only plain URL:

$path = "/home/httpd/html/index.php";
$file = basename($path);         // $file is set to "index.php"
$file = basename($path, ".php"); // $file is set to "index"
  • 写回答

3条回答 默认 最新

  • dpvhv66448 2015-12-10 19:42
    关注

    First of all: use parse_url() as suggested in the comments. If you however opt for a regex solution, consider the following code:

    $str = "http://website-link.com/?page=gf_signature&signature=565dbca63791e5.87676354.png";
    $regex = "/signature=(?<signature>[^&]+)/";
    // that is: match signature= literally, then match everything up to a new ampersand and save it to the group "signature"
    preg_match($regex, $str, $matches);
    $signature = $matches["signature"];
    $ext = substr(strrchr($signature, '.'), 1);
    
    print "Signature: $signature with extension: $ext";
    // prints out: Signature: 565dbca63791e5.87676354.png with extension: png
    

    See a working PHP fiddle here.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 ATAC测序到底用什么peak文件做Diffbind差异分析
  • ¥15 安装ubantu过程中第一个vfat 文件挂载失败
  • ¥20 GZ::CTF如何兼容一些靶机?
  • ¥15 etcd集群部署问题
  • ¥20 谁可以帮我一下问一下各位
  • ¥15 为何重叠加权后love图的SMD与svyCreateTableOne函数绘制基线表的不一致
  • ¥15 QFILHelper怎么恢复全字库,提示进程已完成,只能恢复分区文件
  • ¥150 求 《小魔指》街机游戏机整合模拟软件
  • ¥20 你好,我想问下easyExcel下拉多选,或者复选框可以实现吗
  • ¥20 双非跨考工科哪个专业和方向就业前景好?