如何使用PHP减少/压缩短字符串的长度

时间:2019-10-08 21:17:22

标签: php compression encode

我的客户想要存储长度超过60个字符的文本,但是我不能使用超过60个字符来存储此数据,所以...

我正在寻找一种减少/压缩短文本(少于60个字符)的方法,我测试了gzcompressgzdeflategzencodebzcompress,{ {1}}和base64_encode,但是我获得的字符数等于或大于原始文本。

bin2hex

这些是结果:

$char="This is a sample short text";
echo "<div>$char</div>";
echo "<div>It has ".strlen($char)." characters</div>";

$chargz=gzcompress($char,9);
echo "<div>gzcompress has ".strlen($chargz)." characters</div>";

$chargz=gzdeflate($char,9);
echo "<div>gzdeflate has ".strlen($chargz)." characters</div>";

$chargz=gzencode($char,9);
echo "<div>gzencode has ".strlen($chargz)." characters</div>";

$chargz=bzcompress($char,9);
echo "<div>bzcompress has ".strlen($chargz)." characters</div>";

$chargz=base64_encode($char);
echo "<div>base64_encode has ".strlen($chargz)." characters</div>";

$chargz=bin2hex($char);
echo "<div>bin2hex has ".strlen($chargz)." characters</div>";

您可以看到我并没有真正减少长度,这些方法在较长的文本上可以正常使用,但是短文本呢?

如何减少短弦的长度?

0 个答案:

没有答案