character ranking function implemented in PHP