【问题标题】:PHP array Encoding and Decoding:Need a function for encoding and decoding string or array with delimiters or array itselfPHP数组编码和解码:需要一个函数来编码和解码带有分隔符的字符串或数组或数组本身
【发布时间】:2011-07-16 09:07:46
【问题描述】:

我需要一个对树节点的所有 id 进行编码和解码的函数。 我的函数将检查它是单个字符串还是数组字符串或带有分隔符的字符串。 我有一个用于编码和解码的插件。 这是我的插件:

文件: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;

    }

}

?>

通过使用这个插件中的函数,我必须编写一个函数来检查不同的条件。

【问题讨论】:

  • 请准确告诉我们您要完成的工作。 “检查不同条件”的目标太模糊,我们无法为您提供帮助。
  • 是的,我有一棵树,其中列出了类别和子类别....我需要检查用于编码和解码将进入前端的 id 的不同条件.... ....这里我需要检查单个字符串、字符串数组、带分隔符的字符串......
  • 你一直说你需要检查条件。什么条件?是什么决定了他们?是什么控制着他们?你在说什么样的编码?你的实际最终目标是什么?你在建什么?我们现在所拥有的只是可怕的数据混淆代码,并且不清楚你想用它做什么。
  • sorry....我需要创建 2 个函数....... 1->encode 2->decode ........我是在一个模块上工作,我在树结构中列出所有类别和子类别。在前端,所有类别和子类别的 id 都会出现,我想对其进行编码,而在检索回来时,我希望对 id 进行解码............所以在我的函数中,我想检查条件, 1) id 数组(特定键的多维数组) 2) 单个变量或具有多个 id 的分隔符集的变量...... 3) 使用循环我需要检查这些条件...... .......

标签: php arrays encoding decoding


【解决方案1】:

我为这个基类的扩展类写了一个原型。不确定$PatternFlip 的用途,它在此类中无法访问,因此您必须根据需要进行更改。如果你想要不同格式的结果,你可以在类中添加函数。

class MyProtector extends Protector {

var $Object;
var $encode;//1 means encode, not 1 means decode
var $result;

function __MyProtector($obj="",$encode=0,$pattern="") {
   $this->$Object=$obj;
   if($encode){//encode object
       $this->$EncodePattern=$pattern;
       $this->$result=smartEncode($obj);
   }else{//decode object
       $this->$DecodePattern=$pattern;
       $this->result=smartDecode($obj);
}
}

private function smartEncode($object){
   //encodes string or array
   if(is_array($object){
       return encodeArray($object);
   }else if(is_string($object)){
       return encodeString($object);
   }

   return 0;

}

private function smartDecode($object){
   //encodes string or array
   if(is_string($object)&&strpos("&",$object)===1){
       return encodeDelimiter($object);
   }else if(is_string($object)){
       return encodeString($object);
   }

   return 0;

}
private function decodeDelimiter($object){
   $a=explode("&",$string);//will only work if your encoding does not include "&"!
   $aDecoded=array();
   $k=0; 
   foreach($a as $i){
     $this->toDecode=$i;
     $aDecoded[$k]=$this->Decode();
     $k++;          
   }
   return $aDecoded;


}

private function decodeString($s){
$this->toDecode=$s;
    return $this->Decode();
}

private function encodeString($s){
   $this->toEncode=$s;
   return $this->Encode();
}

private function encodeArray($s){
   foreach($s as $i){
  $this->toEncode=$i;
      $s=$s.$this->Encode()."&";
   }
   return $s;
}
//setters and getters
function getPattern(){
   return $this->Pattern;
}

function getPatternFlip(){
    return $this->Pattern;
}   
function getPattern(){
    return $this->Pattern;
}

//..etc

示例用法:

$o=new MyProtector($string,0,$pattern);
echo $o->$result; //returns encoded string
$o=new MyProtector($string,1,$pattern);
echo $o->$result; //returns decoded string
$o=new MyProtector($array,0,$pattern);
echo $o->$result; //returns decoded string with '&' inbetween encoded array entries
$o=new MyProtector($stringWithDelimiter,1,$pattern);
echo $o->$result;//returns decoded array

我敢肯定会有一些语法错误,因为我还没有编译/尝试过,所以有错别字。希望对您有所帮助。

【讨论】:

    猜你喜欢
    • 2013-05-11
    • 1970-01-01
    • 1970-01-01
    • 2016-01-02
    • 2021-04-20
    • 1970-01-01
    • 2011-10-04
    • 2012-07-05
    • 1970-01-01
    相关资源
    最近更新 更多