My website use various resources from a single domain, for example:
http://static.example.com/javascript/common.js
http://static.example.com/javascript/common.css
http://static.example.com/javascript/menu/menu.js
http://static.example.com/javascript/menu/menu.css
http://static.example.com/images/1804/logo/02000100.jpg
http://static.example.com/images/1804/headers/main/09400060.png
http://static.example.com/images/1804/headers/home/1101/06900200-01.jpg
http://static.example.com/images/1804/headers/home/1101/06900200-02.jpg
I need a very simple string hashing function that maps these URLs to numbers, the numbers being 0, 1, 2 and 3. The algorithm should be deterministic and uniform. I have tagged the question PHP but a generic answer is acceptable.
You might have guessed why I need this; I plan to change the URLs to, for example:
http://0.static.example.com/javascript/common.js
http://2.static.example.com/javascript/common.css
I prefer doing a
crc32hash of the string, and taking its modulo with the limit.Code:
Usage:
Output: