Choose a file to view:
index.php  word.php  functions.php


<?PHP
// connect to database
mysql_connect("localhost""*******""*******");
@
mysql_select_db("bgreco") or die( "Unable to select database");

/* periodic( string $word, array $elements )
 * if $long is true, it will try to use more one-character symbols
 * Returns $word spelled using periodic table symbols
 * Returns false if this is impossible
 */
function periodic($string, &$array$long false) {
  if(
in_array_nocase($string$array) && $long)
    return 
elementCase($string$array);
  if(!
$string)
    return 
false
  
$size1 1;
  
$size2 2;
  if(
$long) {
    
$size1 2;
    
$size2 1;
  }
  
$h1 substr($string0$size1);
  
$h2 substr($string0$size2);
  
$t1 substr($string$size1);
  
$t2 substr($string$size2);
  
  
$r1 periodic($t1$array$long);
  if(
$r1 && in_array_nocase($h1$array))
    return 
elementCase($h1$array).$r1;
  
$r2 periodic($t2$array$long);
  if(
$r2 && in_array_nocase($h2$array))
    return 
elementCase($h2$array).$r2;
  if(
in_array_nocase($string$array) && !$long)
    return 
elementCase($string$array);
  return 
false;
}

function 
save($phrase) {
     
mysql_query(sprintf('INSERT INTO `spelled` (phrase) VALUES ("%s") ON DUPLICATE KEY UPDATE count=count+1'$phrase));
}

function 
countspelled($phrase) {
    
$result mysql_query(sprintf('SELECT count FROM `spelled` WHERE phrase="%s"'$phrase));
    return 
mysql_result($result0);
}

// function taken from the PHP reference manual
function in_array_nocase($search, &$array) {
  
$search strtolower($search);
  foreach (
$array as $item)
    if (
strtolower($item) == $search)
      return 
TRUE;
  return 
FALSE;
}

function 
elementCase($string, &$array) {
  foreach( 
$array as $item) {
    if( 
strtolower($item) == strtolower($string))
      return 
$item;
  }
}

function 
elements() {
  
$elements = array();
  
$result mysql_query("SELECT symbol FROM periodic");
  while(
$row mysql_fetch_row($result))
    
$elements[] = $row[0];
  return 
$elements;
}
?>