php实现的简单压缩英文字符串的代码
更新时间:2008年04月24日 19:28:45 作者:
一直在找压缩字符串的算法,
不知道是不是关键词选择的不对,
找不到适合的,自己写了一对连续字符压缩,(如 vvvv -> 4v)
不过实用效果不太好(压缩比低,效率却不高),
暂且丢上来晒晒吧
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);
}
?>
AWK,通用格式
#!/bin/awk
function compress(str, _ARGVEND_, str_out, str_len, i, s, l) {
str_out = "";
str_len = length(str);
s = "";
l = 1;
for(i =1; i <= str_len; i++) {
if(substr(str, i, 1) == s) l++;
else {
if(s != "") {
if(l > 1) str_out=str_out""l
str_out=str_out""s;
}
s = substr(str, i, 1);
l = 1;
}
}
return str_out;
}
function uncompress(str, _ARGVEND_, str_out, str_len, i, c) {
str_out = "";
str_len = length(str);
for(i =1; i <= str_len; i++) {
c = 0;
while(substr(str, i, 1)~/[0-9]/) {
c = c*10+substr(str, i, 1);
i++;
}
if(c < 1) c = 1;
while(c--) str_out = str_out""substr(str, i, 1);
}
return str_out;
}
复制代码 代码如下:
<?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);
}
?>
AWK,通用格式
复制代码 代码如下:
#!/bin/awk
function compress(str, _ARGVEND_, str_out, str_len, i, s, l) {
str_out = "";
str_len = length(str);
s = "";
l = 1;
for(i =1; i <= str_len; i++) {
if(substr(str, i, 1) == s) l++;
else {
if(s != "") {
if(l > 1) str_out=str_out""l
str_out=str_out""s;
}
s = substr(str, i, 1);
l = 1;
}
}
return str_out;
}
function uncompress(str, _ARGVEND_, str_out, str_len, i, c) {
str_out = "";
str_len = length(str);
for(i =1; i <= str_len; i++) {
c = 0;
while(substr(str, i, 1)~/[0-9]/) {
c = c*10+substr(str, i, 1);
i++;
}
if(c < 1) c = 1;
while(c--) str_out = str_out""substr(str, i, 1);
}
return str_out;
}
相关文章
Laravel执行migrate命令提示:No such file or directory的解决方法
这篇文章主要介绍了Laravel执行migrate命令提示:No such file or directory的解决方法,分析了执行migrate命令出现错误的原因与相关的解决方法,需要的朋友可以参考下2016-03-03
最新评论