Online Text Slugify

Transform a text into a simplified and web-friendly version, slugify a text fast and easy, create friendly URLS online

Text Slugify code snippets

Get Text Slugify code snippets for PHP, JAVA, Python, C# and Visual Basic, just copy and paste the code in your project

function slugify($title) {
    // Convert the title to lowercase
    $slug = strtolower($title);

    // Replace spaces with dashes
    $slug = str_replace(' ', '-', $slug);

    // Remove special characters except dashes and alphanumeric characters
    $slug = preg_replace('/[^a-z0-9-]/', '', $slug);

    // Remove double dashes
    $slug = preg_replace('/-+/', '-', $slug);

    // Trim dashes from the beginning and end
    $slug = trim($slug, '-');

    return $slug;
}
import java.util.regex.Pattern;

public static String slugify(String title) {
    // Convert the title to lowercase
    String slug = title.toLowerCase();

    // Replace spaces with dashes
    slug = slug.replace(" ", "-");

    // Remove special characters except dashes and alphanumeric characters
    slug = slug.replaceAll("[^a-z0-9-]", "");

    // Remove double dashes
    slug = slug.replaceAll("-+", "-");

    // Trim dashes from the beginning and end
    slug = slug.replaceAll("^-|-$", "");

    return slug;
}
import re

def slugify(title):
    # Convert the title to lowercase
    slug = title.lower()
    
    # Replace spaces with dashes
    slug = slug.replace(' ', '-')
    
    # Remove special characters except dashes and alphanumeric characters
    slug = re.sub(r'[^a-z0-9-]', '', slug)
    
    # Remove double dashes
    slug = re.sub(r'-+', '-', slug)
    
    # Trim dashes from the beginning and end
    slug = slug.strip('-')
    
    return slug
using System;
using System.Text.RegularExpressions;

public static string Slugify(string title)
{
    // Convert the title to lowercase
    string slug = title.ToLower();
    
    // Replace spaces with dashes
    slug = slug.Replace(' ', '-');
    
    // Remove special characters except dashes and alphanumeric characters
    slug = Regex.Replace(slug, "[^a-z0-9-]", "");
    
    // Remove double dashes
    slug = Regex.Replace(slug, "-+", "-");
    
    // Trim dashes from the beginning and end
    slug = slug.Trim('-');
    
    return slug;
}
Imports System.Text.RegularExpressions

Public Shared Function Slugify(ByVal title As String) As String
    ' Convert the title to lowercase
    Dim slug As String = title.ToLower()

    ' Replace spaces with dashes
    slug = slug.Replace(" ", "-")

    ' Remove special characters except dashes and alphanumeric characters
    slug = Regex.Replace(slug, "[^a-z0-9-]", "")

    ' Remove double dashes
    slug = Regex.Replace(slug, "-+", "-")

    ' Trim dashes from the beginning and end
    slug = slug.Trim("-"c)

    Return slug
End Function

Slugify is a term used in computer programming to describe a process that turns a text into a simplified and web-friendly version. Imagine you have a title for a blog post or a webpage that has spaces, capital letters, or special characters in it. Slugify helps convert this title into a format that's easier for computers to work with and for web browsers to display. It usually involves changing spaces to dashes, removing special characters, and making everything lowercase. For example, a title like "My Awesome Blog Post!" could become "my-awesome-blog-post" after being slugified. This makes it easier to create web links, as web addresses don't like spaces or special characters. So, slugify is like giving a title a makeover so it can fit better in the digital world.

  1. Convert to Lowercase:

    Input: Original title or string.
    Action: Convert the entire string to lowercase using strtolower.
    Output: A lowercase version of the input string.

  2. Replace Spaces with Dashes:

    Input: Lowercased string from step 1.
    Action: Replace spaces with dashes using str_replace.
    Output: A string with spaces replaced by dashes.

  3. Remove Special Characters:

    Input: String with spaces replaced by dashes.
    Action: Use regular expression preg_replace to remove non-alphanumeric characters.
    Output: A string containing only alphanumeric characters and dashes.

  4. Remove Double Dashes:

    Input: Alphanumeric string with dashes.
    Action: Apply preg_replace to eliminate consecutive dashes.
    Output: A string without consecutive dashes.

  5. Trim Leading and Trailing Dashes:

    Input: String without consecutive dashes.
    Action: Use trim to remove leading or trailing dashes.
    Output: A final slug without leading or trailing dashes.

  6. Return Result:

    Input: Finalized slug.
    Action: Return the resulting slug as the output of the function.
    Output: A clean, URL-friendly slug derived from the original title or string.

Have a question? Send me a message !

Contact me