在字符串中找到所有可能的文件 link 并在其中设置一个下载按钮 link

Find all possible file links in a string and set a download button with a link of them inside

我想更改下面的代码以获取具有所有可能扩展名的所有可能文件,而不仅仅是 exeziprarbin

$find=array('~(<a(.*)href="([^"]*.exe)"(.*)>)([^<]*)(</a>)~',
'~(<a(.*)href="([^"]*.zip)"(.*)>)([^<]*)(</a>)~',
'~(<a(.*)href="([^"]*.rar)"(.*)>)([^<]*)(</a>)~',
'~(<a(.*)href="([^"]*.bin)"(.*)>)([^<]*)(</a>)~');

$matches[$i][1] = preg_replace($find, '<p>[=11=]</p><a href= class="btn btn-success" download>Download</a>',$matches[$i][1]);

理论上有几乎无限数量的文件和扩展名,如果你想捕获所有这些,替换

*.exe 

*.* 

这应该会捕获所有内容

我可以给你一些简单的例子,你可以应用到你的工作中

<a href="(?:\.\/)?(?:[^\/"]+\/)*[^\/"]*\.([a-zA-Z]+)">

此正则表达式将尝试在打开的 <a ...> 标记中捕获文件扩展名。

解释

(?:\.\/)?匹配文件路径前缀./如果存在,

(?:[^\/"]+\/)* 匹配任何以 / 结尾的可能路径,例如this/is/my/path/,

[^\/"]*匹配一个文件名,

\.匹配一个点,

([a-zA-Z]+) 匹配文件扩展名。

DEMO