开发者

Regex matching and encoding duplicate characters in a string

My problem is that I've got URL access keys that look like "Bd333333d". I need the string length to be no longer than the original, but may be shorter. I want to convert/obfuscate the 开发者_高级运维duplicate characters in the string and be able to convert them back to the original.


PHP can already do string compression, so why would you want to come up with your own algorithm? See this post for some excellent suggestions of combining gzip compression with urlencoding.

You don't say whether you're storing these strings internally or using them as part of a URL. If it's the former, then this is even easier because you can just store it as the much more compact binary.


This is a good task for preg_replace_callback

$str = 'Bd333333dddd';

function shorten( $str ) {
    return preg_replace_callback(
        '~(.)\1+~',
        function( $matches ) {
            return sprintf( '%s.%s', $matches[1], strlen( $matches[0] ) );
        },
        $str
    );
}


UPDATE: Thanks for your help! After doing some work on the hybrid ROT13 concept, I came up with something that works for me. Sorry to be lame and post my own solution, but here it is:

function ROT_by_strpos($s,$type='in'){

$index = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

for ($n = 0; $n<strlen($index); $n++){
    $k[] = substr( $index,$n ,1);
}

if($type == 'out'){
    $k = array_reverse($k);
}

$rot = '';
$count = 1;
$len = strlen($s);
for ($n = 0; $n<strlen($s); $n++){
    $key_in[] = substr( $s,$n ,1);
}

for ( $i = 0; $i < $len; $i++ ){
    $key = array_search($key_in[$i], $k)+1;

    if($type == 'in'){
        if($key+$i > count($k)){
            $rev = $key+$i - count($k);
            $new_key = $rev;
        }else{
            $new_key = $key+$i;
        }
    }else{
        if($key+$i >= count($k)){
            $adv = $key+$i - count($k);
            $new_key = $adv;
        }else{
            $new_key = $key+$i;
        }
    }

    $rot .= $k[$new_key];
}

return $rot;
}

This assumes that possible chars are from $index and code string length <= 10 chars long.

Usage:

$key = "Bd333333d";

$in = ROT_by_strpos($key,'in');

$out = ROT_by_strpos($in,'out');

echo "$key - $in - $out"; //Bd333333d - Cf6789ABm - Bd333333d

There's probably a more elegant way to do this, but it does work. Any feedback or improvements would be appreciated if you want to add something. :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜