gpt4 book ai didi

c# - SEO标题制作功能?

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:32:26 25 4
gpt4 key购买 nike

我创建了一个函数,可以将任何字符串转换为制表符分隔的字符串。

What's new in ASP.NET 4.0

然后它将上面的标题转换为以下:

what-s-new-in-asp-net-4-0

我正在使用它来对我的 URL 进行 SEO。但我不确定它是否在所有情况下都能正常工作。到目前为止,我已经在我的数据库中对大约 1000 条记录测试了这个函数,并且它对所有标题都工作正常。 Guyz 请检查此功能并让我知道此功能是否有可能失败,如果此功能有可能失败,请告诉我可以在我的应用程序中使用的正确功能。

public string SEO_makeTitle(object objTitle)
{
string strTitle = Convert.ToString(objTitle);

strTitle = Regex.Replace(strTitle.Trim(), @"\W", " "); //replace special chars
strTitle = Regex.Replace(strTitle.Trim(), @"\s{2,}", " "); //replace double space
strTitle = strTitle.Trim().Replace(" ", "-").ToLower();

return strTitle; //return - delimited title
}

谢谢

最佳答案

您可能需要考虑重音的外观。您正在替换“特殊”字符,但我怀疑其中包括非 ASCII 字母。

我会先尝试将重音字符转换为非重音字符。如果您知道诀窍,那么在 C# 中有一种相对简单的方法可以做到这一点:

 static string RemoveAccents (string input) 
{
string normalized = input.Normalize(NormalizationForm.FormKD);
Encoding removal = Encoding.GetEncoding
(Encoding.ASCII.CodePage,
new EncoderReplacementFallback(""),
new DecoderReplacementFallback(""));
byte[] bytes = removal.GetBytes(normalized);
return Encoding.ASCII.GetString(bytes);
}

您可能还想显式使用 ToLower(CultureInfo.InvariantCulture) 以避免在土耳其运行代码时出现问题。诚然,如果您在运行 RemoveAccents 之前运行 ToLower ,这可能不会成为问题。

关于c# - SEO标题制作功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/810345/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com