php實現的簡單壓縮英文字串的代碼

來源:互聯網
上載者:User

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;
}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.