In my blog app, a user can enter any text as a title for their entry and then I generate a URL based on the text.
I validate their title to make sure it only contains letters and numbers.
If they enter something like
Lorem 3 ipsum dolor sit amet
how could I generate the more SEO friendly version of this text:
Lorem-3-ipsum-dolor-sit-amet
It’s in practice really not as simple as replacing spaces by hypens. You would often also like to make it all lowercase and normalize/replace diacritics, like á, ö, è and so on which are invalid URL characters. The only valid characters are listed as "Unreserved characters" in the 2nd table of this Wikipedia page.
Here’s how such a function can look like:
It does basically the following:
See also: