如何在PHP中压缩/解压缩长查询字符串? [英] How to compress/decompress a long query string in PHP?

查看:95
本文介绍了如何在PHP中压缩/解压缩长查询字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怀疑这是否是加密,但是找不到更好的短语.我需要传递一个长查询字符串,如下所示:

I doubt if this is encryption but I can't find a better phrase. I need to pass a long query string like this:

http://test.com/test.php?key=[some_very_loooooooooooooooooooooooong_query_string]

查询字符串不包含任何敏感信息,因此在这种情况下我并不真正担心安全性.只是,好吧,太长太丑陋了.是否有一个库函数可以让我将查询字符串编码/加密/压缩为类似于md5()的结果(类似于,总是32个字符串),但是可以解码/解密/解压缩?

The query string contains NO sensitive information so I'm not really concerned about security in this case. It's just...well, too long and ugly. Is there a library function that can let me encode/encrypt/compress the query string into something similar to the result of a md5() (similar as in, always a 32 character string), but decode/decrypt/decompress-able?

推荐答案

基本前提非常困难.在URL中传输任何值意味着您只能使用ASCII字符的子集.使用gzcompress之类的任何压缩方式都可以减小字符串的大小,但会导致二进制blob.但是,该二进制blob无法在URL中传输,因为它会产生无效字符.要使用ASCII子集传输该二进制Blob,您需要以某种方式对其进行编码,然后将其转换为ASCII字符.

The basic premise is very difficult. Transporting any value in the URL means you're restricted to a subset of ASCII characters. Using any sort of compression like gzcompress would reduce the size of the string, but result in a binary blob. That binary blob can't be transported in the URL though, since it would produce invalid characters. To transport that binary blob using a subset of ASCII you need to encode it in some way and turn it into ASCII characters.

因此,您需要将ASCII字符转换为其他字符,然后再转换为ASCII字符.

So, you'd turn ASCII characters into something else which you'd then turn into ASCII characters.

但是实际上,大多数时候,您开始使用的ASCII字符已经是最佳长度.这是一个快速测试:

But actually, most of the time the ASCII characters you start out with are already the optimal length. Here a quick test:

$str = 'Hello I am a very very very very long search string';
echo $str . "\n";
echo base64_encode(gzcompress($str, 9)) . "\n";
echo bin2hex(gzcompress($str, 9)) . "\n";
echo urlencode(gzcompress($str, 9)) . "\n";

Hello I am a very very very very long search string
eNrzSM3JyVfwVEjMVUhUKEstqkQncvLz0hWKUxOLkjMUikuKMvPSAc+AEoI=
78daf348cdc9c957f05448cc554854284b2daa442772f2f3d2158a53138b9233148a4b8a32f3d201cf801282
x%DA%F3H%CD%C9%C9W%F0TH%CCUHT%28K-%AAD%27r%F2%F3%D2%15%8AS%13%8B%923%14%8AK%8A2%F3%D2%01%CF%80%12%82

如您所见,原始字符串是最短的.在编码的压缩中,base64最短,因为它使用最大的字母表示二进制数据.不过它仍然比原始的还要长.

As you can see, the original string is the shortest. Among the encoded compressions, base64 is the shortest since it uses the largest alphabet to represent the binary data. It's still longer than the original though.

对于某些非常特殊的字符组合以及一些非常特定的压缩算法,这些算法可以压缩为ASCII可表示的数据,但这有可能实现某些压缩,但这是理论上的. 更新:实际上,这听起来太消极了.问题是您需要确定压缩是否对您的用例有意义.不同的数据压缩方式不同,不同的编码算法工作方式也不同.同样,较长的琴弦可能会实现更好的压缩率.可以达到某些压缩效果的地方可能是一个最佳选择.您需要确定自己是否大部分时间都处于最佳位置.

For some very specific combination of characters with some very specific compression algorithm that compresses to ASCII representable data it may be possible to achieve some compression, but that's rather theoretical. Update: Actually, that sounds too negative. The thing is you need to figure out if compression makes sense for your use case. Different data compresses differently and different encoding algorithms work differently. Also, longer strings may achieve a better compression ratio. There's probably a sweet spot somewhere where some compression can be achieved. You need to figure out if you're in that sweet spot most of the time or not.

像md5之类的东西不合适,因为md5是哈希,这意味着它是不可逆的.您无法从中获取原始值.

Something like md5 is unsuitable since md5 is a hash, which means it's non-reversible. You can't get the original value back from it.

恐怕您只能通过POST发送该参数,如果该参数在URL中不起作用.

I'm afraid you can only send the parameter via POST, if it doesn't work in the URL.

这篇关于如何在PHP中压缩/解压缩长查询字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆