如何在php中实现mysql compress()函数
我想压缩 TEXT 以存储在 MySQL 中。所以我只是在 php 中执行 gzcompress() 然后发送到 mysql,但我还设置了 Sphinx 全文搜索,如果它可以用一个简单的查询填充其索引,那就太好了,但是
select uncompress(thing) from table
我仍然想做应用程序的压缩和解压在php而不是mysql中进行,并且仅使用mysql uncompress()函数进行sphinx索引。
mysql 文档对其压缩功能的描述如下:
非空字符串存储为 未压缩的四字节长度 字符串(低字节在前),然后是 压缩字符串。
所以我的问题是...如何构造这个四字节长度的未压缩字符串?之后,压缩后的 BLOB 看起来就像 php gzcompress() 函数的结果一样。
I want to compress TEXT for storing in MySQL. So I would just do gzcompress() in php and then send to mysql, but I am also setting up Sphinx full text search, and it would be nice if it can populate its index with a simple query i.e.
select uncompress(thing) from table
However I would still like to do compression and decompression for the application in php rather than mysql, and only use the mysql uncompress() function for sphinx indexing.
The mysql documentation says the following about its compress function:
Nonempty strings are stored as a
four-byte length of the uncompressed
string (low byte first), followed by
the compressed string.
So my question is... how do I construct this four-byte length of the uncompressed string? After that, the compressed BLOB looks just like the results of the php gzcompress() function.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
还没有这样做过,但这里有一些想法:
1)找到未压缩字符串的长度... strlen()函数应该工作
2)压缩字符串...你已经完成了这一部分
3)打包两者一起存储在 mysql 中,按照 mysql 的需要格式化数字:
php 的 pack 函数:听起来您需要使用格式值“V”作为长度(无符号长... 32位,小端字节顺序)
haven't ever done this, but here are some thoughts:
1) find the length of the uncompressed string... strlen() function ought to work
2) compress the string... you've already done this part
3) pack both together for storage in mysql, formatting the number as mysql wants it:
php's pack function: sounds like you need to use format value "V" for the length (unsigned long... 32 bit, little endian byte order)
这是我针对该场景的代码,为了解压缩,您还可以使用 PHP 调整前 4 个字节的子字符串。
mysql 的输出:
php 的等价物:
PHP 的输出:
Here is my code for that scenario, for uncompress you can also use PHP an djust substr the first 4 bytes away.
Output of mysql:
The php equivalent:
Output of PHP: