如何在php中对多维数组进行编码和解码?

发布于 2024-10-24 22:15:34 字数 6160 浏览 5 评论 0原文

可能的重复:
PHP数组编码和解码:需要一个用于使用分隔符或数组本身对字符串或数组进行编码和解码的函数...

嗨。 我是 PHP 新手。 我需要编写两个单独的函数来在 php 中对多维数组进行编码和解码。 我已经附加了一个插件......... encode.class.php..............

<?php

/*-------------------------
Author: Jonathan Pulice
Date: July 26th, 2005
Name: JPEncodeClass v1
Desc: Encoder and decoder using patterns.
-------------------------*/

class Protector
{

    var $Pattern = "";
    var $PatternFlip = "";
    var $ToEncode = "";
    var $ToDecode = "";
    var $Decoded = "";
    var $Encoded = "";
    var $Bug = false;
    var $DecodePattern = "";

    function Debug($on = true)
    {
        $this->Bug = $on;
    }

    function Encode()
    {


        $ar = explode(":", $this->Pattern);
        $enc = $this->ToEncode;

        if ($this->Bug) echo "<!-- BEGIN ENCODING -->\n";

        foreach ($ar as $num => $ltr)
        {
            switch ($ltr)
            {
                case "E":
                $enc = base64_encode($enc);
                break;
                case "D":
                $enc = base64_decode($enc);
                break;
                case "R":
                $enc = strrev($enc);
                break;
                case "I":
                $enc = $this->InvertCase($enc);
                break;
            }
            if ($this->Bug) echo "<!-- {$ltr}: {$enc} -->\n";
        }

        if ($this->Bug) echo "<!-------------------->\n\n";

        @$this->Encoded = ($enc == $this->Str) ? "<font color='red'>No Encoding/Decoding Pattern Detected!</font>" : $enc;

        return $this->Encoded;

    }

    function Decode()
    {

        $pattern = ($this->DecodePattern != "") ? $this->DecodePattern : $this->Pattern;

        //Reverse the pattern
        $this->PatternFlip($pattern);

        //make into an array
        $ar = explode(":", $this->PatternFlip);

        $t = ($this->Encoded == "") ? $this->ToDecode : $this->Encoded;

        if ($this->Bug) echo "<!-- BEGIN DECODING -->\n";

        foreach ($ar as $num => $ltr)
        {
        switch ($ltr)
        {
            case "E":
            $t = base64_encode($t);
            break;
            case "D":
            $t = base64_decode($t);
            break;
            case "R":
            $t = strrev($t);
            break;
            case "I":
            $t = $this->InvertCase($t);
            break;
            }
            if ($this->Bug) echo "<!-- {$ltr}: {$t} -->\n";
        }

        if ($this->Bug) echo "<!-------------------->\n\n";

        $this->Decoded = ($t == $this->Encoded) ? "<font color='red'>No Encoding/Decoding Pattern Detected!</font>" : $t;

        return $this->Decoded;

    }

    function MakePattern($len = 10)
    {
        //possible letters
        // E - Base64 Encode
        // R - Reverse String
        // I - Inverse Case
        $poss = array('E','R', 'I');

        //generate a string
        for ( $i = 0 ; $i < $len ; $i++ )
        {
            $tmp[] = $poss[ rand(0,2) ];
        }

        //echo $str. "<br>";
        //fix useless pattern section RR II
        $str = implode(":", $tmp);

        //fix
        $str = str_replace( 'R:R:R:R:R:R' , 'R:E:R:E:R:E' , $str );
        $str = str_replace( 'R:R:R:R:R' , 'R:E:R:E:R' , $str );
        $str = str_replace( 'R:R:R:R' , 'R:E:R:E' , $str );
        $str = str_replace( 'R:R:R' , 'R:E:R' , $str );
        $str = str_replace( 'R:R' , 'R:E' , $str );

        //fix
        $str = str_replace( 'I:I:I:I:I:I' , 'I:E:I:E:I:E' , $str );
        $str = str_replace( 'I:I:I:I:I' , 'I:E:I:E:I' , $str );
        $str = str_replace( 'I:I:I:I' , 'I:E:I:E' , $str );
        $str = str_replace( 'I:I:I' , 'I:E:I' , $str );
        $str = str_replace( 'I:I' , 'I:E' , $str );

        //string is good, set as pattern
        $this->Pattern = $str;
        return $this->Pattern; //if we need it

    }

    function PatternFlip($pattern)
    {
        //reverse the pattern
        $str = strrev($pattern);

        $ar = explode(":", $str);

        foreach ($ar as $num => $ltr)
        {
            switch ($ltr)
            {
                case "E":
                $tmp[] = "D";
                break;
                case "D":
                $tmp[] = "E";
                break;
                case "R":
                $tmp[] = "R";
                break;
                case "I":
                $tmp[] = "I";
                break;
            }

        }

        $rev = implode(":", $tmp);

        $this->PatternFlip = $rev;

        return $this->PatternFlip;
    }

    // This is my custom Case Invertor!
    // if you would like to use this in a script, please credit it to me, thank you
    function InvertCase($str)
    {
        //Do initial conversion
        $new = strtoupper( $str );

        //spluit into arrays
        $s = str_split( $str );
        $n = str_split( $new );

        //now we step through each letter, and if its the same as before, we swap it out
        for ($i = 0; $i < count($s); $i++)
        {
            if ( $s[$i] === $n[$i] ) //SWAP THE LETTER
            {
                //ge the letter
                $num = ord( $n[$i] );

                //see if the ord is in the alpha ranges ( 65 - 90 | 97 - 122 )
                if ( ( $num >= 65 AND $num <= 90 ) OR ( $num >= 97 AND $num <= 122 ) )
                {
                    if ($num < 97 ) { $num = $num + 32; }
                    else { $num = $num - 32; }

                    $newchr = chr($num);

                    $n[$i] = $newchr;
                }
            }
        }

        //join the new string back together
        $newstr = implode("", $n);

        return $newstr;

    }

}

?>............

从这个插件中,我需要在我的函数中使用编码和解码函数进行模式检查。 如果有人可以帮助我......这对我有好处。因为我需要它......

Possible Duplicate:
PHP array Encoding and Decoding:Need a function for encoding and decoding string or array with delimiters or array itself…

hi.
i am new to php.
i need to write two separate functions for encoding and decoding of multidimensional arrays in php.
i have attached one plugin ..........
encode.class.php..............

<?php

/*-------------------------
Author: Jonathan Pulice
Date: July 26th, 2005
Name: JPEncodeClass v1
Desc: Encoder and decoder using patterns.
-------------------------*/

class Protector
{

    var $Pattern = "";
    var $PatternFlip = "";
    var $ToEncode = "";
    var $ToDecode = "";
    var $Decoded = "";
    var $Encoded = "";
    var $Bug = false;
    var $DecodePattern = "";

    function Debug($on = true)
    {
        $this->Bug = $on;
    }

    function Encode()
    {


        $ar = explode(":", $this->Pattern);
        $enc = $this->ToEncode;

        if ($this->Bug) echo "<!-- BEGIN ENCODING -->\n";

        foreach ($ar as $num => $ltr)
        {
            switch ($ltr)
            {
                case "E":
                $enc = base64_encode($enc);
                break;
                case "D":
                $enc = base64_decode($enc);
                break;
                case "R":
                $enc = strrev($enc);
                break;
                case "I":
                $enc = $this->InvertCase($enc);
                break;
            }
            if ($this->Bug) echo "<!-- {$ltr}: {$enc} -->\n";
        }

        if ($this->Bug) echo "<!-------------------->\n\n";

        @$this->Encoded = ($enc == $this->Str) ? "<font color='red'>No Encoding/Decoding Pattern Detected!</font>" : $enc;

        return $this->Encoded;

    }

    function Decode()
    {

        $pattern = ($this->DecodePattern != "") ? $this->DecodePattern : $this->Pattern;

        //Reverse the pattern
        $this->PatternFlip($pattern);

        //make into an array
        $ar = explode(":", $this->PatternFlip);

        $t = ($this->Encoded == "") ? $this->ToDecode : $this->Encoded;

        if ($this->Bug) echo "<!-- BEGIN DECODING -->\n";

        foreach ($ar as $num => $ltr)
        {
        switch ($ltr)
        {
            case "E":
            $t = base64_encode($t);
            break;
            case "D":
            $t = base64_decode($t);
            break;
            case "R":
            $t = strrev($t);
            break;
            case "I":
            $t = $this->InvertCase($t);
            break;
            }
            if ($this->Bug) echo "<!-- {$ltr}: {$t} -->\n";
        }

        if ($this->Bug) echo "<!-------------------->\n\n";

        $this->Decoded = ($t == $this->Encoded) ? "<font color='red'>No Encoding/Decoding Pattern Detected!</font>" : $t;

        return $this->Decoded;

    }

    function MakePattern($len = 10)
    {
        //possible letters
        // E - Base64 Encode
        // R - Reverse String
        // I - Inverse Case
        $poss = array('E','R', 'I');

        //generate a string
        for ( $i = 0 ; $i < $len ; $i++ )
        {
            $tmp[] = $poss[ rand(0,2) ];
        }

        //echo $str. "<br>";
        //fix useless pattern section RR II
        $str = implode(":", $tmp);

        //fix
        $str = str_replace( 'R:R:R:R:R:R' , 'R:E:R:E:R:E' , $str );
        $str = str_replace( 'R:R:R:R:R' , 'R:E:R:E:R' , $str );
        $str = str_replace( 'R:R:R:R' , 'R:E:R:E' , $str );
        $str = str_replace( 'R:R:R' , 'R:E:R' , $str );
        $str = str_replace( 'R:R' , 'R:E' , $str );

        //fix
        $str = str_replace( 'I:I:I:I:I:I' , 'I:E:I:E:I:E' , $str );
        $str = str_replace( 'I:I:I:I:I' , 'I:E:I:E:I' , $str );
        $str = str_replace( 'I:I:I:I' , 'I:E:I:E' , $str );
        $str = str_replace( 'I:I:I' , 'I:E:I' , $str );
        $str = str_replace( 'I:I' , 'I:E' , $str );

        //string is good, set as pattern
        $this->Pattern = $str;
        return $this->Pattern; //if we need it

    }

    function PatternFlip($pattern)
    {
        //reverse the pattern
        $str = strrev($pattern);

        $ar = explode(":", $str);

        foreach ($ar as $num => $ltr)
        {
            switch ($ltr)
            {
                case "E":
                $tmp[] = "D";
                break;
                case "D":
                $tmp[] = "E";
                break;
                case "R":
                $tmp[] = "R";
                break;
                case "I":
                $tmp[] = "I";
                break;
            }

        }

        $rev = implode(":", $tmp);

        $this->PatternFlip = $rev;

        return $this->PatternFlip;
    }

    // This is my custom Case Invertor!
    // if you would like to use this in a script, please credit it to me, thank you
    function InvertCase($str)
    {
        //Do initial conversion
        $new = strtoupper( $str );

        //spluit into arrays
        $s = str_split( $str );
        $n = str_split( $new );

        //now we step through each letter, and if its the same as before, we swap it out
        for ($i = 0; $i < count($s); $i++)
        {
            if ( $s[$i] === $n[$i] ) //SWAP THE LETTER
            {
                //ge the letter
                $num = ord( $n[$i] );

                //see if the ord is in the alpha ranges ( 65 - 90 | 97 - 122 )
                if ( ( $num >= 65 AND $num <= 90 ) OR ( $num >= 97 AND $num <= 122 ) )
                {
                    if ($num < 97 ) { $num = $num + 32; }
                    else { $num = $num - 32; }

                    $newchr = chr($num);

                    $n[$i] = $newchr;
                }
            }
        }

        //join the new string back together
        $newstr = implode("", $n);

        return $newstr;

    }

}

?>............

from this plugin i need to use the encode and decode functions in my functions for pattern checking.
If anybody can help me.......It ll be good for me.coz i am in need of it........

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光清浅 2024-10-31 22:15:34

使用 serialize()unserialize() 将数组转换为字符串并返回数组(也适用于除资源之外的所有其他内容)

Use serialize() and unserialize() to turn the arrays into strings and back into arrays (also works with everything else except resources)

末骤雨初歇 2024-10-31 22:15:34

看起来像一个相当大的编码器/解码器类。

也许这个 RC4 算法会更容易

//encrypt
$encrypted = RC4Crypt::encrypt('your key','some string to encrypt');

//decrypt
$decrypted = RC4Crypt::encrypt('your key',$encrypted); 

//The RC4 Class
 class RC4Crypt {
    function encrypt ($pwd, $data){
        $key[] = '';
        $box[] = '';
        $pwd_length = strlen($pwd);
        $data_length = strlen($data);
        for ($i = 0; $i < 256; $i++){
            $key[$i] = ord($pwd[$i % $pwd_length]);
            $box[$i] = $i;
        }
        for ($j = $i = 0; $i < 256; $i++){
            $j = ($j + $box[$i] + $key[$i]) % 256;
            $tmp = $box[$i];
            $box[$i] = $box[$j];
            $box[$j] = $tmp;
        }
        $cipher = '';
        for ($a = $j = $i = 0; $i < $data_length; $i++){
            $a = ($a + 1) % 256;
            $j = ($j + $box[$a]) % 256;
            $tmp = $box[$a];
            $box[$a] = $box[$j];
            $box[$j] = $tmp;
            $k = $box[(($box[$a] + $box[$j]) % 256)];
            $cipher .= chr(ord($data[$i]) ^ $k);
        }
        return ($cipher);
    }
    function decrypt ($pwd, $data){
        return RC4Crypt::encrypt($pwd, ($data));
    }
} 

seems like a pretty large encoder/decoder class.

maybe this RC4 algorithm would be easier

//encrypt
$encrypted = RC4Crypt::encrypt('your key','some string to encrypt');

//decrypt
$decrypted = RC4Crypt::encrypt('your key',$encrypted); 

//The RC4 Class
 class RC4Crypt {
    function encrypt ($pwd, $data){
        $key[] = '';
        $box[] = '';
        $pwd_length = strlen($pwd);
        $data_length = strlen($data);
        for ($i = 0; $i < 256; $i++){
            $key[$i] = ord($pwd[$i % $pwd_length]);
            $box[$i] = $i;
        }
        for ($j = $i = 0; $i < 256; $i++){
            $j = ($j + $box[$i] + $key[$i]) % 256;
            $tmp = $box[$i];
            $box[$i] = $box[$j];
            $box[$j] = $tmp;
        }
        $cipher = '';
        for ($a = $j = $i = 0; $i < $data_length; $i++){
            $a = ($a + 1) % 256;
            $j = ($j + $box[$a]) % 256;
            $tmp = $box[$a];
            $box[$a] = $box[$j];
            $box[$j] = $tmp;
            $k = $box[(($box[$a] + $box[$j]) % 256)];
            $cipher .= chr(ord($data[$i]) ^ $k);
        }
        return ($cipher);
    }
    function decrypt ($pwd, $data){
        return RC4Crypt::encrypt($pwd, ($data));
    }
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文