PHP Coding
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsPHP RelatedPHP Coding

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here!
  #1  
Old September 12th, 2002, 05:52 PM
ResNet9 ResNet9 is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 101 ResNet9 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
Send a message via AIM to ResNet9
Cryptogram

Okay...my girlfriend likes doing cryptograms so I thought it would be a good chance to work on some string manipulation if I made her a dynamic web app to do these. Eventually it is going to pull quotes from a database so that I won't have to enter in a new one all the time. I'm having a little trouble with the algorithms to shuffle the quote around on a index alphabet. Hopefully the code will help it make more sense. I think that I am close but it isn't encoding the quote completely correctly yet if you see the out put of the code.
[CODE]
<?
$str1 = "As you can clearly see from this image, without salsa,
Leonard Nimoy's projected level of excellence is not even controllable!
However, as we suspected, with the benefit of salsa, Leonard Nimoy's
level of excellence will be unstoppable!";
//$str1Arrary[] = array();

//for ($i = 0; $i < count($str1); $i++)
//{
//$str1Array[$i] = $str1[$i];
//}

$str1 = strtolower($str1);
echo $str1;

echo '<br />';
echo '<br />';

$beta = array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m",
"n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z");

$alpha = array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m",
"n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z");

//shuffles the alphabet
for($j = 0; $j < count($alpha); $j++)
{
$num = rand(0, 25);

$temp = $alpha[$j];
$alpha[$j] = $alpha[$num];
$alpha[$num] = $temp;
}

//displays the shuffled alphabet
for($k = 0; $k < count($alpha); $k++)
{
echo $alpha[$k];
}

echo '<br />';
echo '<br />';

for($w = 0; $w < strlen($str1); $w++)
{
for($z = 0; $z < count($alpha); $z++)
{
if ($str1[$w] == $alpha[$z])
{
$str1[$w] = $beta[$z];
}
}
}

for($s = 0; $s < strlen($str1); $s++)
{
echo $str1[$s];
}
?>

Reply With Quote
  #2  
Old September 12th, 2002, 06:41 PM
notepad notepad is offline
Codewalkers Loyal (3000 - 3499 posts)
 
Join Date: Apr 2007
Location: Central, IL USA
Posts: 3,214 notepad User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
Send a message via AIM to notepad
RE: Cryptogram

i'm not to familiar with cryptograph's.. are you just shuffling words around or the letters of the words around? what is it supposed to do?

Reply With Quote
  #3  
Old September 12th, 2002, 10:21 PM
ResNet9 ResNet9 is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 101 ResNet9 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
Send a message via AIM to ResNet9
RE: Cryptogram

In cryptograms letters are given different values. ie. shuffles the alphabet

(PHP is fun) ===>
(XMX hq jop)

then the person has to decode a phrase

Reply With Quote
  #4  
Old September 12th, 2002, 11:02 PM
notepad notepad is offline
Codewalkers Loyal (3000 - 3499 posts)
 
Join Date: Apr 2007
Location: Central, IL USA
Posts: 3,214 notepad User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
Send a message via AIM to notepad
RE: Cryptogram

ohh cool well.. here you go

edit: new code below

it works okay but replaces comma's and stuff with letters too but i'm off work which means i'm not staying late to finish

Reply With Quote
  #5  
Old September 12th, 2002, 11:39 PM
ResNet9 ResNet9 is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 101 ResNet9 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
Send a message via AIM to ResNet9
RE: Cryptogram

sounds good. Thanks for you help. For some reason the way I was doing it worked fine as long as the word didn't have 2 of the same letters in it. Hopefully this will keep her busy so I can have more time developing and what not.

Reply With Quote
  #6  
Old September 12th, 2002, 11:40 PM
ResNet9 ResNet9 is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 101 ResNet9 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
Send a message via AIM to ResNet9
RE: Cryptogram

PS- Where is Creve Coeur, IL?
I'm from Arlington Heights, IL

Reply With Quote
  #7  
Old September 13th, 2002, 02:08 AM
notepad notepad is offline
Codewalkers Loyal (3000 - 3499 posts)
 
Join Date: Apr 2007
Location: Central, IL USA
Posts: 3,214 notepad User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
Send a message via AIM to notepad
RE: Cryptogram

alright the following code will work with comma's, apostrophies or whatever ..and, i'm not sure where arlington heights is but it sounds REALLY familiar so it can't be that far away.. creve coeur is right around pekin/peoria/washington area
php Code:
Original - php Code
  1.  
  2. <?php
  3.  
  4. $str = "As you can clearly see from this image, without salsa,
  5. Leonard Nimoy's projected level of excellence is not even controllable!
  6. However, as we suspected, with the benefit of salsa, Leonard Nimoy's
  7. level of excellence will be unstoppable!";
  8.  
  9. $str = strtolower($str);
  10. $words = explode(" ", $str);
  11. $alpha = range("a", "z");
  12.  
  13. foreach($words as $key=>$value)
  14. {
  15.     for($i=0; $i<strlen($value); $i++)
  16.     {
  17.         if(in_array($value{$i}, $alpha))
  18.         {
  19.             $value{$i} = $alpha[rand(0, count($alpha))];
  20.         }
  21.     }
  22.     $words[$key] = $value;
  23. }
  24.  
  25. $newstr = implode(" ", $words);
  26.  
  27. echo "<b>old string:</b> $str<br><br>n";
  28. echo "<b>new string:</b> $newstr<br><br>n";
  29.  
  30. ?>

Reply With Quote
  #8  
Old September 14th, 2002, 12:44 AM
ResNet9 ResNet9 is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 101 ResNet9 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
Send a message via AIM to ResNet9
RE: Cryptogram

I see I see. You've probably heard of Arlington Heights because of the horse track...that is why most people recognize

Reply With Quote
  #9  
Old September 16th, 2002, 04:49 AM
blazin blazin is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Aurora, CO, USA
Posts: 24 blazin User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to blazin Send a message via AIM to blazin
RE: Cryptogram

ResNet9:

Here's what I think you are looking for. This code will create a valid cryptogram and keep the letter case intact. The problem with Notepad's code is that while it does to the letter substitution, your girlfriend will be very frustrated since 'a' may mean 'b' in one place and mean 'q' or 'c' some other place. For a cryptogram to be solvable, it needs to have consistent substitutions.

php Code:
Original - php Code
  1.  
  2. <?php
  3. $str = "As you can clearly see from this image, without salsa,
  4. Leonard Nimoy's projected level of excellence is not even controllable!
  5. However, as we suspected, with the benefit of salsa, Leonard Nimoy's
  6. level of excellence will be unstoppable!";
  7.  
  8. $newstr = ($str);
  9. $alpha = range("a", "z");
  10.  
  11. srand ((float)microtime()*1000000);
  12. $alpha = ShuffleArray($alpha);
  13.  
  14. for ($i = 0; $i < strlen($str); $i++)
  15. {
  16.     if (ord($str[$i]) >= ord('a') && ord($str[$i])<= ord('z'))
  17.     {
  18.         $newstr[$i] = $alpha[ord($str[$i])-97];
  19.     }
  20.     else if (ord($str[$i]) >= ord('A') && ord($str[$i])<=ord('Z'))
  21.     {
  22.         $newstr[$i] = strtoupper($alpha[ord(strtolower($str[$i]))-97]);
  23.     }
  24.     else
  25.     {
  26.         $newstr[$i] = $str[$i];
  27.     }
  28. }
  29.  
  30. echo "<b>old string:</b> $str<br><br>n";
  31. echo "<b>new string:</b> $newstr<br><br>n";
  32.  
  33. function ShuffleArray($array)
  34. {
  35.     $caa=(count($array)-1);
  36.     $new_array=$array;
  37.     for ($x=0;$x<=$caa;$x++)
  38.     {
  39.         $ca=(count($array)-1);
  40.         $i=rand(0,$ca);
  41.         $new_array[$x]=$array[$i];
  42.         for ($t=$i;$t<$ca;$t++)
  43.         $array[$t]=$array[$t+1];
  44.         array_pop($array);
  45.     }
  46.     return $new_array;
  47. }
  48.  
  49. ?>


Anyway, I hope this helps. The ArrayShuffle routine is in there since I found that PHP's shuffle function doesn't work very well. The ArrayShuffle function seems to work much better, and you should get a different cryptogram for the phrase every time you reload.

Dave

Reply With Quote
  #10  
Old September 16th, 2002, 05:25 AM
notepad notepad is offline
Codewalkers Loyal (3000 - 3499 posts)
 
Join Date: Apr 2007
Location: Central, IL USA
Posts: 3,214 notepad User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
Send a message via AIM to notepad
RE: Cryptogram

ahh.. see i didn't know any better

Reply With Quote
  #11  
Old September 16th, 2002, 02:45 PM
blazin blazin is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Aurora, CO, USA
Posts: 24 blazin User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to blazin Send a message via AIM to blazin
RE: Cryptogram

Not a problem notepad. I figured that's all the problem was.

Dave