-1

I am working on hashing a string and getting a hasnumber and then unhasing a number to get a string back. Below is my java version of it:

public class Hashcode {
  private static final String ALPHABET = "acegikmnoprstuvy";

  public static long hash(String s) {
    long h = 7;
    for (int i = 0; i < s.length(); i++) {
      h = (h * 37 + ALPHABET.indexOf(s.charAt(i)));
    }
    return h;
  }

  public static String unhash(long n) {
    String result = "";
    while (n > 7) {
      result = ALPHABET.charAt((int) (n % 37)) + result;
      n = n / 37;
    }
    if (n != 7) {
      System.err.println("Error, hash parity incorrect.");
      System.exit(1);
    }
    return result;
  }

  public static void main(String[] args) {
    System.out.println(hash("reports"));
    System.out.println(unhash(690336378753L));
    System.out.println(unhash(932246728227799L));
    System.out.println(hash("mymitsapp"));
  }
}

Now I am trying to do same exercise in php as shown below but it is giving me an error:

function unhash($h) {  
    $letters = "acegikmnoprstuvy";
    $s='';
    while ($h>7){
        //hold
        $pos = fmod($h, 37);
        $s = $letters[$pos].$s;
        $h = ($h-$pos) / 37 ;
    }
    return $s;
}

Below is an error and here is the ideone link.

PHP Parse error: syntax error, unexpected end of file, expecting ',' or ';' in /home/mNw4IK/prog.php on line 16

Any thoughts what is wrong or any other better way to write this?

chris85
  • 23,846
  • 7
  • 34
  • 51
user1234
  • 145
  • 1
  • 12

1 Answers1

0

The error is saying that it is expecting either , or ;.

You have:

echo $something 

but it should be:

echo $something;

Hope this helps!

Rwd
  • 34,180
  • 6
  • 64
  • 78