PHP Hash Chinese Character or String

April 22nd, 2013 by xrigher Leave a reply »

Hash a Chinese character or string into a integer number. This might be useful when one wants to split a huge table which is indexed by Chinese strings horizontally into many tables.

	/**
	 * Hash a chinese charactor into an int number.
	 * @param string $c A chinese character
	 * @return number
	 */
	public static function hashZhChar($c) {
		return (ord(substr($c, 0 , 1)) -176)*94 + ord(substr($c, 1, 1)) - 161;
	}
	/**
	 * Hash a chinese string into an integer number.
	 *
	 * @param string $s A chinese string.
	 * @return number
	 */
	static function hashZh($s) {
		$first = mb_substr($s, 0, 1, 'UTF-8');
		$last = mb_substr($s, -1, 1, 'UTF-8');
		$middle = mb_substr($s, intval(mb_strlen($s, 'UTF-8')/2), 1, 'UTF-8');
		return self::hashZhChar($first) + self::hashZhChar($last) + self::hashZhChar($middle);
	}
	

Advertisement

Leave a Reply