例子,php实现字符串压缩的简单代码。
<?php
//replacement来自上个版本的加密替换
function compress_func($match) {return strlen($match[0]).$match[0]{0};}
function uncompress_func($match) {return str_repeat($match[2], $match[1]);}
function compress($str) {
$i = 0;
$pattern = array();
while(isset($replacement{$i})) array_push($pattern, "/".$replacement{$i++}."{2,}/");
return preg_replace_callback($pattern, "compress_func", $str);
}
function uncompress($str) {
return preg_replace_callback("/(d+)(w)/", "uncompress_func", $str);
}
?>
下面再分享个linuxjishu/13830.html target=_blank class=infotextkey>awk实现字符串压缩的代码。
AWK,通用格式压缩字符串: