[英]How can I convert a query string into a a shorter alphanumeric string (and convert it back again) in PHP?
I want to store the query string of the current URL as a shorter alphanumeric string with the ability to convert it back to the original query string. 我想将当前URL的查询字符串存储为较短的字母数字字符串,并能够将其转换回原始查询字符串。
For example: inputType=timeline&count=50&hashtag=%23li&filterspecifiedhashtag=1&filterhashtagsend=1&filterscreennames=1&extracturl=1&deshortifyurl=1&filterurls=1
例如:
inputType=timeline&count=50&hashtag=%23li&filterspecifiedhashtag=1&filterhashtagsend=1&filterscreennames=1&extracturl=1&deshortifyurl=1&filterurls=1
I want to be able to use the resultant alphanumeric string as a filename. 我希望能够将结果字母数字字符串用作文件名。
I want to avoid using MYSQL or storing values in a text file. 我想避免使用MYSQL或将值存储在文本文件中。
Is there a way to convert to an alphanumeric string as well as being able to convert it back to the original query string? 有没有一种方法可以转换为字母数字字符串,也可以将其转换回原始查询字符串? I am not very knowledgeable about hashing but wondering whether some kind of "two way hashing" technique could work?
我对哈希不是很了解,但想知道某种“双向哈希”技术是否可以工作?
You can use base64_encode() and base64_decode(), but if you want it as a filename, you'll likely hit filename length limit of filesystem (255 chars for ext3). 您可以使用base64_encode()和base64_decode(),但如果要将其用作文件名,则很可能会达到文件系统的文件名长度限制(ext3为255个字符)。 If it's possible you hit this limit, you can use each X chars as directory name, and create full path.
如果有可能达到此限制,则可以将每个X字符用作目录名,并创建完整路径。
What you are seeking is not a hash - since hash is a one-way function in common case. 您要查找的不是哈希-因为在通常情况下哈希是一种单向函数。 Here is a possible solution - use both base64 encryption and something like parameters map, so you will able to get shorter file name because you'll not store parameters names, only values:
这是一个可能的解决方案-同时使用base64加密和类似参数映射的方法,因此您将获得较短的文件名,因为您将不存储参数名,而仅存储值:
class Holder
{
const NAME_PARAM_DELIMIER = '|';
public static function getParametersMap()
{
return [
0 => 'count',
1 => 'deshortifyurl',
2 => 'extracturl',
3 => 'filterhashtagsend',
4 => 'filterscreennames',
5 => 'filterspecifiedhashtag',
6 => 'filterurls',
7 => 'hashtag',
8 => 'inputType',
];
}
public static function getParamsByName($sName, $bReturnAsArray=true)
{
$rgParams = @array_combine(self::getParametersMap(), explode(self::NAME_PARAM_DELIMIER, base64_decode($sName)));
if(!is_array($rgParams))
{
return null;
}
return $bReturnAsArray?$rgParams:http_build_query($rgParams);
}
public static function getNameByParams($sQuery)
{
parse_str($sQuery, $rgParams);
ksort($rgParams);
return base64_encode(join(self::NAME_PARAM_DELIMIER, array_values($rgParams)));
}
}
$sQuery = 'inputType=timeline&count=50&hashtag=%23li&filterspecifiedhashtag=1&filterhashtagsend=1&filterscreennames=1&extracturl=1&deshortifyurl=1&filterurls=1';
$sName = Holder::getNameByParams($sQuery);
$rgData = Holder::getParamsByName($sName);
var_dump($sName); //NTB8MXwxfDF8MXwxfDF8I2xpfHRpbWVsaW5l
var_dump($rgData);
Also note that base64 will produce "=" symbols - I am not sure that it is allowed on all file systems (I'm using Reiser, so it's ok in my case) 还要注意,base64将产生“ =”符号-我不确定在所有文件系统上都允许它(我使用的是Reiser,所以就我而言是可以的)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.