php gzcompress() 和gzuncompress()函数实现字符串压缩
时间:2023年10月05日
/来源:网络
/编辑:佚名
::(滑稽) 当我们说到压缩,我们可能会想到文件压缩,其实,字符串也是可以压缩的。
PHP提供了 gzcompress() 和gzuncompress() 函数:
$string = “Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc ut elit id mi
ultricies adipiscing. Nulla facilisi. Praesent pulvinar, sapien vel feugiat vestibulum,
nulla dui pretium orci, non ultricies elit lacus quis ante. Lorem ipsum dolor sit amet,
consectetur adipiscing elit. Aliquam pretium ullamcorper urna quis iaculis. Etiam ac
massased turpis tempor luctus. Curabitur sed nibh eu elitmollis congue. Praesent ipsum diam,
consectetur vitae ornare a, aliquam a nunc. In id magna pellentesque tellus posuere
adipiscing. Sed non mi metus, at lacinia augue. Sed magna nisi, ornare in mollis in,
mollis sed nunc. Etiam at justo in leo congue mollis.Nullam in neque eget metus hendrerit
scelerisque eu non enim. Ut malesuada lacus eu nulla bibendum id euismod urna sodales. “;
/*压缩字符串*/
$compressed = gzcompress($string);
echo “Original size: “. strlen($string).” ”;
/* 输出原始大小 Original size: 800 */
echo “Compressed size: “. strlen($compressed).” ”;
/* 输出压缩后的大小 Compressed size: 418 */
// 解压缩
$original = gzuncompress($compressed);
PHP提供了 gzcompress() 和gzuncompress() 函数:
$string = “Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc ut elit id mi
ultricies adipiscing. Nulla facilisi. Praesent pulvinar, sapien vel feugiat vestibulum,
nulla dui pretium orci, non ultricies elit lacus quis ante. Lorem ipsum dolor sit amet,
consectetur adipiscing elit. Aliquam pretium ullamcorper urna quis iaculis. Etiam ac
massased turpis tempor luctus. Curabitur sed nibh eu elitmollis congue. Praesent ipsum diam,
consectetur vitae ornare a, aliquam a nunc. In id magna pellentesque tellus posuere
adipiscing. Sed non mi metus, at lacinia augue. Sed magna nisi, ornare in mollis in,
mollis sed nunc. Etiam at justo in leo congue mollis.Nullam in neque eget metus hendrerit
scelerisque eu non enim. Ut malesuada lacus eu nulla bibendum id euismod urna sodales. “;
/*压缩字符串*/
$compressed = gzcompress($string);
echo “Original size: “. strlen($string).” ”;
/* 输出原始大小 Original size: 800 */
echo “Compressed size: “. strlen($compressed).” ”;
/* 输出压缩后的大小 Compressed size: 418 */
// 解压缩
$original = gzuncompress($compressed);
新闻资讯 更多
- 【建站知识】查询nginx日志状态码大于400的请求并打印整行04-03
- 【建站知识】Python中的logger和handler到底是个什么?04-03
- 【建站知识】python3拉勾网爬虫之(您操作太频繁,请稍后访问)04-03
- 【建站知识】xpath 获取meta里的keywords及description的方法04-03
- 【建站知识】python向上取整以50为界04-03
- 【建站知识】scrapy xpath遇见乱码解决04-03
- 【建站知识】scrapy爬取后中文乱码,解决word转为html 时cp1252编码问题04-03
- 【建站知识】scrapy采集—爬取中文乱码,gb2312转为utf-804-03