正如Kasia Gogolek所建议的那样,你可能会对PHP提供的<编码>str_replace(功能感兴趣。 你可以这样利用:
$username = str_replace(I , l ,$username);
或者,你可能希望利用Levenshtein远程功能确定最接近的$username
到名词典中显示。 只有在数据库没有匹配的情况下,你才能使用词典,即使你取代减少资源使用的特性。 PHP有这种功能,其名称是:levenshtein(
-http://php.net/manual/en/Function.levenshtein.php>rel=“nofollow” 。
如果你能够确定贵轮功店主的储存程序,你可以采用以下例行做法(根据MySQL.5.16测试):
CREATE FUNCTION `LEVENSHTEIN`(`s1` VARCHAR(255), `s2` VARCHAR(255))
RETURNS int(11)
LANGUAGE SQL
DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT
BEGIN
DECLARE s1_len, s2_len, i, j, c, c_temp, cost INT;
DECLARE s1_char CHAR;
DECLARE cv0, cv1 VARBINARY(256);
SET s1_len = CHAR_LENGTH(s1), s2_len = CHAR_LENGTH(s2), cv1 = "0×00", j = 1, i = 1, c = 0;
IF s1 = s2 THEN
RETURN 0;
ELSEIF s1_len = 0 THEN
RETURN s2_len;
ELSEIF s2_len = 0 THEN
RETURN s1_len;
ELSE
WHILE j <= s2_len DO
SET cv1 = CONCAT(cv1, UNHEX(HEX(j))), j = j + 1;
END WHILE;
WHILE i <= s1_len DO
SET s1_char = SUBSTRING(s1, i, 1), c = i, cv0 = UNHEX(HEX(i)), j = 1;
WHILE j <= s2_len DO
SET c = c + 1;
IF s1_char = SUBSTRING(s2, j, 1) THEN SET cost = 0; ELSE SET cost = 1; END IF;
SET c_temp = CONV(HEX(SUBSTRING(cv1, j, 1)), 16, 10) + cost;
IF c > c_temp THEN SET c = c_temp; END IF;
SET c_temp = CONV(HEX(SUBSTRING(cv1, j+1, 1)), 16, 10) + 1;
IF c > c_temp THEN SET c = c_temp; END IF;
SET cv0 = CONCAT(cv0, UNHEX(HEX(c))), j = j + 1;
END WHILE;
SET cv1 = cv0, i = i + 1;
END WHILE;
END IF;
RETURN c;
END
您的问:
SELECT t1.`key` FROM `dict` AS t1 WHERE levenshtein("patryk", t1.`key`) < 5
<patryk>
是 用户名
变量。
EDIT
I have just checked the query against the example input you provided (DaryII
and Daryll
) and it returns the correct row.
EDIT 2
You might also be interested in a percentage ratio. Use this function instead then:
CREATE FUNCTION levenshtein_ratio( s1 VARCHAR(255), s2 VARCHAR(255) )
RETURNS INT
DETERMINISTIC
BEGIN
DECLARE s1_len, s2_len, max_len INT;
SET s1_len = LENGTH(s1), s2_len = LENGTH(s2);
IF s1_len > s2_len THEN
SET max_len = s1_len;
ELSE
SET max_len = s2_len;
END IF;
RETURN ROUND((1 - LEVENSHTEIN(s1, s2) / max_len) * 100);
END;
这两项职能均从