Hi Eric,
Regrettably, Kentico
CMS does not provide any general method to get safe string text. However, there are two methods to get safe file name and part of url:
CMS.GlobalHelper.UrlHelper.GetSafeFileName
CMS.GlobalHelper.UrlHelper.GetSafeUrlPartMoreover, there should not be any problem to create your custom method, which could get Forbidden URL characters and Forbidden characters replacement values using following method:
CMS.GlobalHelper.UrlHelper.ForbiddenURLCharacters
CMS.GlobalHelper.UrlHelper.ForbiddenCharactersReplacementand could replace the forbidden characters in the text. So in general, this method could look as follows:
public string GetSafeString(string text, string siteName)
{
// If text is not specified, do not process
if (text == null)
{
return "";
}
// Remove diacritics from the url
text = TextHelper.RemoveDiacritics(text.Trim());
// Get site forbidden character and repalcement
string forbiddenChars = CMS.GlobalHelper.UrlHelper.ForbiddenURLCharacters(siteName);
char replacement = CMS.GlobalHelper.UrlHelper.ForbiddenCharactersReplacement(siteName);
string repalcementString = replacement.ToString();
// Replace the forbidden characters
for (int i = 0; i <= forbiddenChars.Length - 1; i++)
{
text = text.Replace(forbiddenChars, replacement);
}
return text;
}
Best regards,
Michal Legen