esposimi

embedded_words.php

Mar 29th, 2011
410
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.43 KB | None | 0 0
  1. <?php
  2.     $Phrases = array(
  3.         "Your Chinese zodiac tells a lot about your personality.",
  4.         "Embed PHP Scripts within an XHTML document.");
  5.    
  6.     $SignNames = array(
  7.         "Rat",
  8.         "Ox",
  9.         "Tiger",
  10.         "Rabbit",
  11.         "Dragon",
  12.         "Snake",
  13.         "Horse",
  14.         "Goat",
  15.         "Monkey",
  16.         "Rooster",
  17.         "Dog",
  18.         "Pig");
  19.    
  20.     function BuildLetterCounts($text){
  21.         $text = strtoupper($text);
  22.         $letter_counts = count_chars($text);
  23.         return $letter_counts;
  24.     }
  25.    
  26.     function AContainsB($A, $B){
  27.         $retval = TRUE;
  28.         $first_letter_index = ord('A');
  29.         $last_letter_index = ord('Z');
  30.         for ($letter_index = $first_letter_index;
  31.                 $letter_index <= $last_letter_index;
  32.                 ++$letter_index){
  33.                     if ($A[$letter_index] < $B[$letter_index]) {
  34.                         $retval = FALSE;
  35.                     }
  36.                 }
  37.             return $retval;
  38.     }
  39.    
  40.     foreach ($Phrases as $Phrase) {
  41.         $PhraseArray = BuildLetterCounts($Phrase);
  42.         $GoodWords = array();
  43.         $BadWords = array();
  44.     }
  45.    
  46.     foreach ($SignNames as $Word) {
  47.         $WordArray = BuildLetterCounts($Word);
  48.         if (AContainsB($PhraseArray, $WordArray))
  49.             $GoodWords[] = $Word;
  50.         else
  51.             $BadWords[] = $Word;
  52.     }
  53.    
  54.     echo "<p>The following words can be made from the letters in the phrase &quot;$Phrase&quot;:";
  55.     foreach ($GoodWords As $Word)
  56.         echo " $Word";
  57.     echo "</p>\n";
  58.     echo "<p>The following words can NOT be made from the letters in the phrase &quot;$Phrase&quot;:";
  59.     foreach ($BadWords as $Word)
  60.         echo " $Word";
  61.     echo "</p>\n";
  62.     echo "<hr />\n";
  63. ?>
Advertisement
Add Comment
Please, Sign In to add comment