[英]I want to retrieve the filename without extension from an url with php
我有一個URL,其中包含各種POST-DATA和一個圖像文件。 我的鏈接是: http://website-link.com/?page=gf_signature&signature=565dbca63791e5.87676354.png
: http://website-link.com/?page=gf_signature&signature=565dbca63791e5.87676354.png
我想將565dbca63791e5.87676354.png
與URL分開,並將擴展名(.png)與URL分開。
我可以做到的:但是它僅來自純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"
$filename = $_REQUEST['signature'];
$pathinfo = pathinfo($filename);
$pathinfo['filename']
將輸出565dbca63791e5.87676354
而$pathinfo['extension']
將為png
。
如果我正確理解你,那么你需要一些這樣的功能
$arr = explode('.', $_REQUEST['signature']);
function arrayFilter($arr){
foreach($arr as $key=>$item){
if(!next($arr)){
$result['extension'] = $item;
} else {
$result['value'] .= $item . '.';
}
}
$result['value'] = substr($result['value'], 0, -1);
return $result;
}
$data= arrayFilter($arr);
並打印[value] => 565dbca63791e5.87676354 [extension] => png
首先:按照注釋中的建議使用parse_url()
。 但是,如果您選擇正則表達式解決方案,請考慮以下代碼:
$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
在這里查看有效的PHP小提琴。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.