Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I'm using URL Routing and I found this works well, pass each part of your URL to this function. It's more than you need as it converts characters like "&amp;" to "and", but you can modify it to suit:</p> <pre><code>public static string CleanUrl(this string urlpart) { // convert accented characters to regular ones string cleaned = urlpart.Trim().anglicized(); // do some pretty conversions cleaned = Regex.Replace(cleaned, "&amp;nbsp;", "-"); cleaned = Regex.Replace(cleaned, "#", "no."); cleaned = Regex.Replace(cleaned, "&amp;", "and"); cleaned = Regex.Replace(cleaned, "%", "percent"); cleaned = Regex.Replace(cleaned, "@", "at"); // strip all illegal characters like punctuation cleaned = Regex.Replace(cleaned, "[^A-Za-z0-9- ]", ""); // convert spaces to dashes cleaned = Regex.Replace(cleaned, " +", "-"); // If we're left with nothing after everything is stripped and cleaned if (cleaned.Length == 0) cleaned = "no-description"; // return lowercased string return cleaned.ToLower(); } // Convert accented characters to standardized ones private static string anglicized(this string urlpart) { string beforeConversion = "àÀâÂäÄáÁéÉèÈêÊëËìÌîÎïÏòÒôÔöÖùÙûÛüÜçÇ’ñ"; string afterConversion = "aAaAaAaAeEeEeEeEiIiIiIoOoOoOuUuUuUcC'n"; string cleaned = urlpart; for (int i = 0; i &lt; beforeConversion.Length; i++) { cleaned = Regex.Replace(urlpart, afterConversion[i].ToString(), afterConversion[i].ToString()); } return cleaned; // Spanish : ÁÉÍÑÓÚÜ¡¿áéíñóúü" } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload