2011-03-16 1 views
0

트리 노드의 모든 ID를 인코딩하고 디코딩하는 함수가 필요합니다. 단일 문자열이나 배열 문자열 또는 구분 기호가있는 문자열인지 확인하는 함수입니다. 인코딩 및 디코딩을위한 플러그인이 하나 있습니다. 나는 다른 조건을 확인하는 기능을 작성해야이 플러그인에서 기능을 사용하여 encode.class.phpPHP 배열 인코딩 및 디코딩 : 구분 기호 또는 배열 자체를 사용하여 문자열 또는 배열을 인코딩 및 디코딩하는 데 필요한 함수가 필요합니다.

<?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; 

    } 

} 

?> 

:

파일 : 여기에 내 플러그인을 간다.

+1

정확 하시겠습니까? –

+0

정확히 무엇을 성취하려고하는지 알려주십시오. "다른 조건 확인"은 우리가 당신을 도울 목표가 너무 모호합니다. – Charles

+0

나중에 나는 범주와 하위 범주를 나열하는 트리가 있습니다. .... 나는 ID를 인코딩하고 디코딩하는 다른 조건을 확인해야합니다. 프런트 엔드 ........ 여기에 필요합니다. 단일 문자열, 문자열 배열, 구분 기호가있는 문자열을 확인하려면 ...... – vk1985

답변

0

이 기본 클래스의 확장 클래스에 대한 프로토 타입을 작성했습니다. 무엇이 $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 

나는 몇 가지 구문 오류가있을 것입니다 확신이있다 내가 컴파일하지 않은로 오타/일을 시도했다. 그것이 도움이되기를 바랍니다.