仅回溯文件名,不能扩展但功能错误 [英] Retriving only file name without extension but function error
问题描述
我有一个包含各种POST数据的url和最后一个图片文件。
我的示例链接是: http://website-link.com/?page=gf_signature&signature=565dbca63791e5.87676354.png
I have an url which contains various POST-DATA in it And an image file at last.
My example link is : http://website-link.com/?page=gf_signature&signature=565dbca63791e5.87676354.png
我想从URL中分离 565dbca63791e5.87676354.png
,并将其分隔符(.png)。
I want to seperate the 565dbca63791e5.87676354.png
from the url and seperate the extension (.png) from it.
我试过这个:
I have tried this:
<?php
$images = array();
$imagesNew = array();
$imgUrls = array(
'ptSignature' => 'http://website-link.com/?page=gf_signature&signature=5668695879dc84.35037895.png',
'pSignature' => 'http://website-link.com/?page=gf_signature&signature=5668694f80aa55.79055562.png',
'witness1Signature' => 'http://website-link.com/?page=gf_signature&signature=5668695875c6e5.03917128.png',
'witness2Signature' => 'http://website-link.com/?page=gf_signature&signature=5668695879dc84.35037895.png',
)
function make_without_ext($str)
{
$regex = "/signature=(?<signature>[^&]+)/";
preg_match($regex, $str, $matches);
$signature = $matches["signature"];
$ext = substr(strrchr($signature, '.'), 1);
$without_extension = basename($signature, '.png');
return $without_extension;
}
foreach ($imgUrls as $imgUrl) {
$imgWithoutExt = make_without_ext($imgUrl);
array_push($images, $imgWithoutExt);
}
foreach ($images as $image) {
$content = file_get_contents($image);
$data = base64_encode($content);
array_push($imagesNew, $data)
}
print '<pre>';
print_r ($imagesNew);
print '<pre>';
但它显示语法错误,意外的'function'(T_FUNCTION) code>
But it shows syntax error, unexpected 'function' (T_FUNCTION)
推荐答案
我为这样的事情所做的是使用PHP的 explode 功能。使用你的代码,我会这样做:
What I do for things like this is use PHP's explode function. Using your code, I would do something like this:
$str_parts = explode('.', $str);
$extension = array_pop($str_parts);
请注意,假设您有一个。在你的字符串!然后,你可以返回你的原始字符串没有这个扩展使用substr或东西。如果签名总是你的url中的最后一个参数,你可以使用相同的技术来抓住它,如果你想要的话,尽管可能有更好的方法来使用 parse_url 。
Note that this assumes you have a . in your string! Then you can just return your original string without this extension using substr or something. If signature is always the last parameter in your url, you could use the same technique to grab it if you wanted to although there are probably better ways to do that like using parse_url.
更好的工作示例
better working example
<?php
$url = "http://website-link.com/?page=gf_signature&signature=565dbca63791e5.87676354.png";
// to keep query only
$a = explode('&', parse_url($url, PHP_URL_QUERY));
$count = 0;
// looping parameters
foreach($a as $as => $that){
if(preg_match('|signature=|', $a[$count]) != false){
// this is like, if this string is contained in this string do that
$b = trim($a[$count], 'signature='); // to remove the part we dont want
$b = trim($b, '.png'); // again
echo $b; // voilà!
}
$count++;
}
?>
我必须承认这个 foreach($ a as $ as => $那){
是我在循环播放的一种..
I must confess this foreach($a as $as => $that){
is kind of me trolling on a loop..
这篇关于仅回溯文件名,不能扩展但功能错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!