如何匹配url中可以以querystring结尾的文件扩展名
How match file extension in url that can end with querystring?
我只想匹配以下所有URL中的文件扩展名直到问号。因此URL#4将匹配"file.pdf"中的pdf,但不匹配"otherfile.exe"中的"exe"。
http://www.someplace.com/directory/file.pdf
http://www.someplace.com/directory/file.pdf?otherstuff=true
http://www.someplace.com/directory/file.pdf?other=true&more=false
http://www.someplace.com/directory/file.pdf?other=true&more=false&value=otherfile.exe
我该怎么做?
我试过了,但不起作用:
([^'.]+)('?|[^'?]$)+
这将是我使用的版本
/'w+'.[A-Za-z]{3,4}(?='?|$)/
这是一个工作版本:
http://regex101.com/r/sY2fR0/1
使用"向前看"?或者字符串(?='?|$)
的末尾,您就可以匹配它后面的内容
$re = "/''w+''.[A-Za-z]{3,4}(?=''?|$)/";
$str = "http://www.someplace.com/directory/file.pdf?other=true&more=false&value=otherfile.exe'n'n";
preg_match($re, $str, $matches);
对于匹配,请尝试此不区分大小写的函数:
function matchURLs($desiredURL, $compareURL){
$url = parse_url($compareURL);
if(preg_match('/^'$url['scheme'].'://'.$url['host'].$url['path'].'$/i', $desiredURL)){
return true;
}
return false;
}
matchURLs('http://www.someplace.com/directory/file.pdf', 'http://www.someplace.com/directory/file.exe'); // false
matchURLs('http://www.someplace.com/directory/file.pdf', 'http://www.someplace.com/directory/file.pdf?value=file.exe'); // true
仅在?
:之前获取
function URL_before_query($url){
$u = parse_url($url);
return $u['scheme'].'://'.$u['host'].$u['path'];
}
echo URL_before_query('http://www.someplace.com/directory/file.pdf?other=true&more=false&value=otherfile.exe'); // http://www.someplace.com/directory/file.pdf
<?
$str = '
http://www.someplace.com/directory/file1.pdf
http://www.someplace.com/directory/file2.pdf?otherstuff=true
http://www.someplace.com/directory/file3.pdf?other=true&more=false
http://www.someplace.com/directory/file4.pdf?other=true&more=false&value=otherfile.exe
';
$regex= '~.*/'K[^?'n]+~';
preg_match_all($regex, $str, $out, PREG_SET_ORDER);
print_r($out);
?>
输出
Array (
[0] => Array (
[0] => file1.pdf
)
[1] => Array (
[0] => file2.pdf
)
[2] => Array (
[0] => file3.pdf
)
[3] => Array (
[0] => file4.pdf
)
)
相关文章:
- 如何播放部分音频文件
- 从桌面读取python文件时高亮显示代码
- 使用JS如何动态更改显示的html文件中的文本背景颜色
- 如何使用WCF服务和javascript表单post上传.doc文件
- 当包含另一个asp文件时,是否也包含所有引用的样式和脚本页面
- 如何在生成下载文件时显示加载动画
- 有没有任何方法可以将控制器从文件加载到ui路由器$stateProvider中
- Javascript-如何读取json文件中的列并将其保存在Javascript数组中
- 将jsp文件下拉列表中的选定项分配给一个java变量(比如String selection)
- 正在SharePoint 2013母版页中添加JQuery移动文件
- 如何在php文件中获取$.post-ajax传递的值
- 直接下载文件,而不是从window.open(url)
- 如何在android中使用phonegap将文件从一个文件夹移动/复制到另一个文件夹
- Angular http.get没有'我不喜欢在开头和结尾加方括号的文本文件
- 咕噜咕噜:在整个文件中附加行.在文件的开头和结尾
- 如何匹配url中可以以querystring结尾的文件扩展名
- Regexp以匹配以Javascript结尾的文件
- 用javascript找到文本文件的结尾
- 如何替换文件的结尾