Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP array Encoding and Decoding:Need a function for encoding and decoding string or array with delimiters or array itself
    primarykey
    data
    text
    <p>I am in a need of function that will encode and decode all the id's of a tree nodes. I function that will check if its a single string or array string or string with delimiters. I am having one plugin for encoding and decoding. here goes my plugin:</p> <p><strong>FIle:encode.class.php</strong></p> <pre><code>&lt;?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-&gt;Bug = $on; } function Encode() { $ar = explode(":", $this-&gt;Pattern); $enc = $this-&gt;ToEncode; if ($this-&gt;Bug) echo "&lt;!-- BEGIN ENCODING --&gt;\n"; foreach ($ar as $num =&gt; $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-&gt;InvertCase($enc); break; } if ($this-&gt;Bug) echo "&lt;!-- {$ltr}: {$enc} --&gt;\n"; } if ($this-&gt;Bug) echo "&lt;!--------------------&gt;\n\n"; @$this-&gt;Encoded = ($enc == $this-&gt;Str) ? "&lt;font color='red'&gt;No Encoding/Decoding Pattern Detected!&lt;/font&gt;" : $enc; return $this-&gt;Encoded; } function Decode() { $pattern = ($this-&gt;DecodePattern != "") ? $this-&gt;DecodePattern : $this-&gt;Pattern; //Reverse the pattern $this-&gt;PatternFlip($pattern); //make into an array $ar = explode(":", $this-&gt;PatternFlip); $t = ($this-&gt;Encoded == "") ? $this-&gt;ToDecode : $this-&gt;Encoded; if ($this-&gt;Bug) echo "&lt;!-- BEGIN DECODING --&gt;\n"; foreach ($ar as $num =&gt; $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-&gt;InvertCase($t); break; } if ($this-&gt;Bug) echo "&lt;!-- {$ltr}: {$t} --&gt;\n"; } if ($this-&gt;Bug) echo "&lt;!--------------------&gt;\n\n"; $this-&gt;Decoded = ($t == $this-&gt;Encoded) ? "&lt;font color='red'&gt;No Encoding/Decoding Pattern Detected!&lt;/font&gt;" : $t; return $this-&gt;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 &lt; $len ; $i++ ) { $tmp[] = $poss[ rand(0,2) ]; } //echo $str. "&lt;br&gt;"; //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-&gt;Pattern = $str; return $this-&gt;Pattern; //if we need it } function PatternFlip($pattern) { //reverse the pattern $str = strrev($pattern); $ar = explode(":", $str); foreach ($ar as $num =&gt; $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-&gt;PatternFlip = $rev; return $this-&gt;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 &lt; 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 &gt;= 65 AND $num &lt;= 90 ) OR ( $num &gt;= 97 AND $num &lt;= 122 ) ) { if ($num &lt; 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; } } ?&gt; </code></pre> <p>By using the functions from this plugin I have to write a function to check for different conditions.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload