Online slug service generator
The slug is used to construct friendly url, slug is easy to write, easy to understand and descriptive.
The slug is a portion of the URL that corresponds to a specific resource, is usually generated in a text string (usually the title of the page).
The slug has the characteristic to be formed by words divided by a separator, usually the dash (-).
Why Clean urls?
The slug creation systems do some simple steps:
- Convert all lowercase letters.
- Are converted (transliterate) all the letters that can not be used in a url (https://en.wikipedia.org/wiki/Percent-encoding#Types_of_URI_characters),for example (è) becomes (e), or (Ù) becomes (u).
- The empty spaces are converted from the separator (usually -).
- The characters that cannot be converted are removed.
For example, the text "How Are You?'m Fine, thanks!" becomes "how-are-you-i-m-fine-thanks".
A url with slug will then be much easier to interpret by a web surfer to click on that link.
Here are two url in comparison, the first without the use of slug, the second with the use of a slug:
https://example.com/index.php?page=125 https://example.com/contact-us
contact-us is the slug, easy to remember, easy to see what page we are going to access.
Sites that use slug are also taken into account by the search engines which is better able to categorize the pages.
Pages with slug are therefore much more seo friendly.
Modern frameworks or cms like Wordpress or Symfony using the integrated systems of generation of the slug, usually starting from the headline or the name of the page.
Slug Libraries
Php
There are several classes or function for PHP.
Here some packages from Composer.
This is my favourute: https://github.com/andyhu/transliteration
/** * Modifies a string to remove all non ASCII characters and spaces. */ static public function slugify($text) { // replace non letter or digits by - $text = preg_replace('~[^\\pL\d]+~u', ' -', $text); // trim $text = trim($text, '-'); // transliterate if (function_exists('iconv')) { $text = iconv('utf-8', 'us - ascii//TRANSLIT', $text); } // lowercase $text = strtolower($text); // remove unwanted characters $text = preg_replace('~[^-\w]+~', '', $text); if (empty($text)) { return 'n-a'; } return $text; }
Slugify function in Javascript
var slugifyText = function (text) { return text.toString().toLowerCase() .replace(/\s+/g, '-') .replace(/[^\w\-]+/g, '') .replace(/\-\-+/g, '-') .replace(/^-+/, '') .replace(/-+$/, ''); }
Slugify function in C#
private string ToSeoFriendly(string title, int maxLength) { var match = Regex.Match(title.ToLower(), "[\\w]+"); StringBuilder result = new StringBuilder(""); bool maxLengthHit = false; while (match.Success && !maxLengthHit) { if (result.Length + match.Value.LengthSlugify class in Java
package util; import java.text.Normalizer; import java.text.Normalizer.Form; import java.util.Locale; import java.util.regex.Pattern; public class Slug { private static final Pattern NONLATIN = Pattern.compile("[^\\w-]"); private static final Pattern WHITESPACE = Pattern.compile("[\\s]"); public String makeSlug(String input) { String nowhitespace = WHITESPACE.matcher(input).replaceAll("-"); String normalized = Normalizer.normalize(nowhitespace, Form.NFD); String slug = NONLATIN.matcher(normalized).replaceAll(""); return slug.toLowerCase(Locale.ENGLISH); } }Slugify class in Perl
my $input = 'In C#: How do I add "Quotes" around string in a comma delimited list of strings?'; my $length = 20; $input =~ s/[^a-z0-9]+/-/gi; $input =~ s/^(.{1,$length}).*/\L$1/; print "$input\n";Slugify class in Ruby
def seo_friendly(str) str.strip.downcase.gsub /\W+/, '-' end