压缩字符串,最终结果没有换行符?

发布于 2024-08-09 22:12:33 字数 206 浏览 3 评论 0原文

我正在尝试将任何给定的字符串压缩为较短的版本,复制不包含任何换行符的可粘贴压缩字符串。

我尝试了 gzcompress,但然后将结果复制/粘贴到不同的 php 脚本中,并尝试 gzuncompress 抛出“警告:gzuncompress():数据错误”

是否有任何本机 php 函数可以压缩字符串,并且结果是一个没有任何字符串的字符串换行?

谢谢。

I'm trying to compress any given string to a shorter version, copy paste-able compressed string that doesn't contain any line breaks.

I tried gzcompress, but then copy/pasting the result into a different php script and trying to gzuncompress throws "Warning: gzuncompress(): data error"

Is there any native php function that compresses a string, and the result is a string without any line breaks?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

怎会甘心 2024-08-16 22:12:33

您可以尝试base64_encode/base64_decode。如果您要压缩为二进制以进行剪切和粘贴,我建议您首先对其进行 Base64 编码。

You could try base64_encode / base64_decode. If you're compressing to binary for cut and paste, I would suggest you base64 encode it first.

等风来 2024-08-16 22:12:33

您可以在压缩后转义换行符:在字符串上运行 gzcompress(),用压缩结果中已知的 2 个字符对替换换行符。要解压缩,请用换行符替换已知的 2 个字符对,然后运行 ​​gzuncompress()...

实际上,您需要执行 2 次替换。因为我无法用英语(不是我的母语)表达这一点,所以这里有一个示例:使用“+n”来转义换行符。您首先需要转义每个独立的“+”,因为如果它后面跟着“n”,那么在解压缩时它会被意外地替换为换行符;我们选择“++”来转义“+”。然后用“+n”替换换行符。解压缩时,将每个“+n”对替换为换行符,然后将每个“++”对替换为“+”。就是这样 !

you can escape your line-breaks after compressing: run gzcompress() on your string, replace line-breaks with a known 2 characters pair in the compressed result. to uncompress, replace the known 2 characters pair by line-breaks, then run gzuncompress()...

actually, you will need to perform 2 replacements. since i can't express this in english (not my native tongue), here is an example: use '+n' to escape line-breaks. you will first need to escape every '+' which are standing alone, since if it is followed by a 'n' it will be accidentally replaced by a line-break when uncompressing; let's chose '++' for escaping '+. then replace line-breaks by '+n'. when uncompressing, replace every '+n' pair by a line-break, then every '++' pair by '+'. that's it !

淡淡離愁欲言轉身 2024-08-16 22:12:33

设计一种总是产生比输入短的输出的通用压缩算法是不可能的。因此,如果您总是希望输出比输入短,则必须开始限制算法的功能。您需要考虑输入(长)字符串中哪些字符是可接受的,以及输出(短)字符串中哪些字符是可接受的。一旦您对这些有了很好的了解,您就可以开始考虑您的选择。

It's impossible to design a general compression algorithm which always produces output shorter than the input. So, if you always want shorted output than input, you have to start restricting what your algorithm can do. You need to think about which characters are acceptable in the input (long) string, and which characters are acceptable in your output (short) string. Once you have a good idea of these, you can start working out what your options are.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文