Roadkill.Core.Mvc.ViewModels.PageViewModel.EncodePageTitle C# (CSharp) 메소드

EncodePageTitle() 공개 정적인 메소드

Removes all bad characters (ones which cannot be used in a URL for a page) from a page title.
public static EncodePageTitle ( string title ) : string
title string
리턴 string
        public static string EncodePageTitle(string title)
        {
            if (string.IsNullOrEmpty(title))
                return title;

            // Search engine friendly slug routine with help from http://www.intrepidstudios.com/blog/2009/2/10/function-to-generate-a-url-friendly-string.aspx

            // remove invalid characters
            title = Regex.Replace(title, @"[^\w\d\s-]", "");  // this is unicode safe, but may need to revert back to 'a-zA-Z0-9', need to check spec

            // convert multiple spaces/hyphens into one space       
            title = Regex.Replace(title, @"[\s-]+", " ").Trim();

            // If it's over 30 chars, take the first 30.
            title = title.Substring(0, title.Length <= 75 ? title.Length : 75).Trim();

            // hyphenate spaces
            title = Regex.Replace(title, @"\s", "-");

            return title;
        }