<?php function picketFenceString($string)
{
$isOdd = (strlen($string) % 2); //Determine if length is even or odd
$oddFlag = false;
if ($isOdd == 0) //String is Even
{
$intLen = strlen($string);
}
elseif($isOdd == 1) // String is odd
{
$intLen = (strlen($string) - 1);
$oddFlag = true;
}
$charArray = str_split($string); //Break our string into an array of each character.
$first=array();
$last=array();
$count=0;
foreach ($charArray as $key=>$val)
{
if (($key) >= ($intLen/2)) //Halfway through origional string, so populate second array now
{
$last[$count] = $val; //We don't want our second array to start at anything other than 0, so we don't recycle the index like we do witht he first array
$count++;
}
else
{
$first[$key] = $val;
}
}
// Now we have two two arrays, of a string cut in half.
// Keep in mind there are many many ways to perform this encryption, simple as it may be
// But let us move on to the actual "picketing".
// My goal when writing this was to take these two arrays, and place the second array into every other slot of the first array, and expanding it.
// So that coding becomes [c,o,d] [i,n,g]
// then becomes [c,i,o,n,d,g]
// If the string is odd in length, such as programming, then we'll throw that in the front of the string.
// this will preserve the string pretty well, so keep in mind taht this alone is a weak encryption
$finalArray = array();
$flipBit = false; //Determines which array to pull from, toggles each loop execution. For readability, I seperate the toggle
$firstEx = true; //Since we throw the last character into the front, we want a flag for the first loop execution
$firstIndex = 0;
$lastIndex = 0;
for($i=0;$i<=$intLen;$i++)
{
if ($oddFlag && $firstEx){
$finalArray[$i] = $charArray[$intLen];
$firstEx=false;
}
else //Not an odd string (anymore), so continue
{
if (!$flipBit)
{
$finalArray[$i] = $first[$firstIndex];
$firstIndex++;
}
else
{
$finalArray[$i] = $last[$lastIndex];
$lastIndex++;
}
}
if ($flipBit){$flipBit=false;} // toggle
elseif (!$flipBit){$flipBit=true;}
}
return $finalArray;
}
function undoPicketFenceString($string)
{
$isOdd = (strlen($string) % 2); //Determine if length is even or odd
$oddFlag = false;
if ($isOdd == 0) //String is Even
{
$intLen = strlen($string);
}
elseif($isOdd == 1) // String is odd
{
$intLen = (strlen($string) - 1);
$oddFlag = true;
}
$charArray = str_split($string); //Break our string into an array of each character.
$first=array();
$last=array();
$count=0;
$finalArray = array();
$flipBit = false; //Determines which array to pull from, toggles each loop execution. For readability, I seperate the toggle
$firstEx = true; //Since we throw the first character into the back, we want a flag for the first loop execution
$firstIndex = 0;
$lastIndex = 0;
for($i=0;$i<=$intLen;$i++)
{
if ($oddFlag && $firstEx){
$last[$intLen+1] = $charArray[0];
$firstEx=false;
}
else //Not an odd string (anymore), so continue
{
if (!$flipBit)
{
$last[$lastIndex] = $charArray[$i];
$lastIndex++;
}
else
{
$first[$firstIndex] = $charArray[$i];
$firstIndex++;
}
}
if ($flipBit){$flipBit=false;} // toggle
elseif (!$flipBit){$flipBit=true;}
}
$finalFirst = implode("", $last); //now to combine the arrays, we need two strings
$finalLast = implode("", $first);
$finalString = $finalFirst.$finalLast; //at this point, our string is 99.999% origional, however its its odd the first char will be incorrect, so lets fix that.
if ($oddFlag)
{
$firstChar = substr($finalString, 0,1);
$finalString = substr($finalString, 1);
$finalString = $finalString.$firstChar;
}
return $finalString;
}
$text = "Programming is so much fun! *cues cheering crowd*";
$text2 = implode("", picketFenceString($text));
print ("Origional: <strong>".$text."</strong>");
print ("<br>After: <strong>".$text2."</strong>");
//print ("<br>After: <strong>".implode("", undoPicketFenceString($text2))."</strong>");
print ("<br>After: <strong>".undoPicketFenceString($text2)."</strong>");
?>