Skip to content

Instantly share code, notes, and snippets.

View overtrue's full-sized avatar
🎯
Focusing

安正超 overtrue

🎯
Focusing
View GitHub Profile
@erning
erning / hz2py.md
Created November 4, 2011 05:51
汉字转拼音

汉字转拼音 (hz2py)

我们希望安居客的搜索引擎能够更好的做到同音字的容错,采用拼音容错是一个不错的方法。因此,需要一个将汉字转换为拼音的组件。同时,汉字转拼音组件还可以有多个用途,例如以拼音的首字母来检索小区名、人名等。

这样我们需要一个通用的将汉字转换为拼音的服务。

功能

基本功能就是中文拉丁化,输入一段中文文本,输出转变为汉语拼音的文本。

要求原文中的全角标点符号、空格等应该转为对应的半脚符号。原汉字与英文间如果没有空格分隔,转换为拼音后应该加入空格分隔。

@troelskn
troelskn / gist:1287893
Created October 14, 2011 18:24
Luhn's algorithm in php
<?php
function is_valid_luhn($number) {
settype($number, 'string');
$sumTable = array(
array(0,1,2,3,4,5,6,7,8,9),
array(0,2,4,6,8,1,3,5,7,9));
$sum = 0;
$flip = 0;
for ($i = strlen($number) - 1; $i >= 0; $i--) {
$sum += $sumTable[$flip++ & 0x1][$number[$i]];