Chuyển tiếng Việt có dấu sang không dấu
Ứng dụng của hàm này thường dùng trong những trường hợp sau:
- Tạo URL thân thiện
- Tạo Desciption tối ưu bộ tìm kiếm
C# - Cách 1
private static readonly string[] VietNamChar = new string[]
{
"aAeEoOuUiIdDyY",
"áàạảãâấầậẩẫăắằặẳẵ",
"ÁÀẠẢÃÂẤẦẬẨẪĂẮẰẶẲẴ",
"éèẹẻẽêếềệểễ",
"ÉÈẸẺẼÊẾỀỆỂỄ",
"óòọỏõôốồộổỗơớờợởỡ",
"ÓÒỌỎÕÔỐỒỘỔỖƠỚỜỢỞỠ",
"úùụủũưứừựửữ",
"ÚÙỤỦŨƯỨỪỰỬỮ",
"íìịỉĩ",
"ÍÌỊỈĨ",
"đ",
"Đ",
"ýỳỵỷỹ",
"ÝỲỴỶỸ"
};
public static string utf8Convert1(string str)
{
//Thay thế và lọc dấu từng char
for (int i = 1; i < VietNamChar.Length; i++)
{
for (int j = 0; j < VietNamChar[i].Length; j++)
str = str.Replace(VietNamChar[i][j], VietNamChar[0][i - 1]);
}
return str;
}
C# - Cách 2
public string utf8Convert(string s)
{
string stFormD = s.Normalize(NormalizationForm.FormD);
StringBuilder sb = new StringBuilder();
for (int ich = 0; ich < stFormD.Length; ich++)
{
System.Globalization.UnicodeCategory uc = System.Globalization.CharUnicodeInfo.GetUnicodeCategory(stFormD[ich]);
if (uc != System.Globalization.UnicodeCategory.NonSpacingMark)
{
sb.Append(stFormD[ich]);
}
}
sb = sb.Replace('Đ', 'D');
sb = sb.Replace('đ', 'd');
return (sb.ToString().Normalize(NormalizationForm.FormD));
}
C# - Cách 3
public static string utf8Convert3(string s)
{
Regex regex = new Regex("\\p{IsCombiningDiacriticalMarks}+");
string temp = s.Normalize(NormalizationForm.FormD);
return regex.Replace(temp, String.Empty).Replace('\u0111', 'd').Replace('\u0110', 'D');
}
JavaScript
function utf8ConvertJavascript(obj)
{
var str;
str = str.toLowerCase();
str = str.replace(/à|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ/g, "a");
str = str.replace(/è|é|ẹ|ẻ|ẽ|ê|ề|ế|ệ|ể|ễ/g, "e");
str = str.replace(/ì|í|ị|ỉ|ĩ/g, "i");
str = str.replace(/ò|ó|ọ|ỏ|õ|ô|ồ|ố|ộ|ổ|ỗ|ơ|ờ|ớ|ợ|ở|ỡ/g, "o");
str = str.replace(/ù|ú|ụ|ủ|ũ|ư|ừ|ứ|ự|ử|ữ/g, "u");
str = str.replace(/ỳ|ý|ỵ|ỷ|ỹ/g, "y");
str = str.replace(/đ/g, "d");
str= str.replace(/!|@|%|\^|\*|\(|\)|\+|\=|\<|\>|\?|\/|,|\.|\:|\;|\'| |\"|\&|\#|\[|\]|~|$|_/g,"-");
/* tìm và thay thế các kí tự đặc biệt trong chuỗi sang kí tự - */
str= str.replace(/-+-/g,"-"); //thay thế 2- thành 1-
str = str.replace(/^\-+|\-+$/g, "");
return str;
}
SQL Server - Cách 1
CREATE FUNCTION [dbo].[utf8ConvertSQL]
(
@strInput nvarchar(4000)
)
RETURNS nvarchar(4000)
AS
BEGIN
SET @strInput = RTRIM(LTRIM(LOWER(@strInput)));
IF @strInput IS NULL
BEGIN
RETURN @strInput;
END;
IF @strInput = ''
BEGIN
RETURN @strInput;
END;
DECLARE @text nvarchar(50), @i int;
SET @text = '-''`~!@#$%^&*()?><:|}{,./\"''='';–';
SELECT @i = PATINDEX('%['+@text+']%', @strInput);
WHILE @i > 0
BEGIN
SET @strInput = replace(@strInput, SUBSTRING(@strInput, @i, 1), '');
SET @i = PATINDEX('%['+@text+']%', @strInput);
END;
SET @strInput = replace(@strInput, ' ', ' ');
DECLARE @RT nvarchar(4000);
DECLARE @SIGN_CHARS nchar(136);
DECLARE @UNSIGN_CHARS nchar(136);
SET @SIGN_CHARS = N'ăâđêôơưàảãạáằẳẵặắầẩẫậấèẻẽẹéềểễệế
ìỉĩịíòỏõọóồổỗộốờởỡợớùủũụúừửữựứỳỷỹỵý'+NCHAR(272)+NCHAR(208);
SET @UNSIGN_CHARS = N'aadeoouaaaaaaaaaaaaaaaeeeeeeeeee
iiiiiooooooooooooooouuuuuuuuuuyyyyy';
DECLARE @COUNTER int;
DECLARE @COUNTER1 int;
SET @COUNTER = 1;
WHILE(@COUNTER <= LEN(@strInput))
BEGIN
SET @COUNTER1 = 1;
WHILE(@COUNTER1 <= LEN(@SIGN_CHARS) + 1)
BEGIN
IF UNICODE(SUBSTRING(@SIGN_CHARS, @COUNTER1, 1)) = UNICODE(SUBSTRING(@strInput, @COUNTER, 1))
BEGIN
IF @COUNTER = 1
BEGIN
SET @strInput = SUBSTRING(@UNSIGN_CHARS, @COUNTER1, 1) + SUBSTRING(@strInput, @COUNTER+1, LEN(@strInput)-1);
END;
ELSE
BEGIN
SET @strInput = SUBSTRING(@strInput, 1, @COUNTER-1) + SUBSTRING(@UNSIGN_CHARS, @COUNTER1, 1) + SUBSTRING(@strInput, @COUNTER+1, LEN(@strInput)-@COUNTER);
END;
BREAK;
END;
SET @COUNTER1 = @COUNTER1 + 1;
END;
SET @COUNTER = @COUNTER + 1;
END;
SET @strInput = replace(@strInput, ' ', '-');
RETURN LOWER(@strInput);
END;
SQL Server - Cách 2
CREATE FUNCTION [dbo].[GetUnsignString]
(
@strInput nvarchar(4000)
)
RETURNS nvarchar(4000)
AS
BEGIN
IF @strInput IS NULL
BEGIN
RETURN @strInput
END;
IF @strInput = ''
BEGIN
RETURN @strInput
END;
DECLARE @RT nvarchar(4000);
DECLARE @SIGN_CHARS nchar(136);
DECLARE @UNSIGN_CHARS nchar(136);
SET @SIGN_CHARS = N'ăâđêôơưàảãạáằẳẵặắầẩẫậấèẻẽẹéềểễệếìỉĩịíòỏõọóồổỗộốờởỡợớùủũụúừửữựứỳỷỹỵýĂÂĐÊÔƠƯÀẢÃẠÁẰẲẴẶẮẦẨẪẬẤÈẺẼẸÉỀỂỄỆẾÌỈĨỊÍÒỎÕỌÓỒỔỖỘỐỜỞỠỢỚÙỦŨỤÚỪỬỮỰỨỲỶỸỴÝ'+NCHAR(272)+NCHAR(208);
SET @UNSIGN_CHARS = N'aadeoouaaaaaaaaaaaaaaaeeeeeeeeeeiiiiiooooooooooooooouuuuuuuuuuyyyyyAADEOOUAAAAAAAAAAAAAAAEEEEEEEEEEIIIIIOOOOOOOOOOOOOOOUUUUUUUUUUYYYYYDD';
DECLARE @COUNTER int;
DECLARE @COUNTER1 int;
SET @COUNTER = 1;
WHILE(@COUNTER <= LEN(@strInput))
BEGIN
SET @COUNTER1 = 1;
WHILE(@COUNTER1 <= LEN(@SIGN_CHARS) + 1)
BEGIN
IF UNICODE(SUBSTRING(@SIGN_CHARS, @COUNTER1, 1)) = UNICODE(SUBSTRING(@strInput, @COUNTER, 1))
BEGIN
IF @COUNTER = 1
BEGIN
SET @strInput = SUBSTRING(@UNSIGN_CHARS, @COUNTER1, 1) + SUBSTRING(@strInput, @COUNTER+1, LEN(@strInput)-1);
END
ELSE
BEGIN
SET @strInput = SUBSTRING(@strInput, 1, @COUNTER-1) + SUBSTRING(@UNSIGN_CHARS, @COUNTER1, 1) + SUBSTRING(@strInput, @COUNTER+1, LEN(@strInput)-@COUNTER);
END;
BREAK;
END;
SET @COUNTER1 = @COUNTER1 + 1;
END;
SET @COUNTER = @COUNTER + 1;
END;
RETURN @strInput;
END;