on Tips

How to generate URL friendly title

5 comments
IMAG0025
Tweet about this on TwitterShare on FacebookShare on Google+Share on LinkedInShare on RedditShare on StumbleUpon

The problem with titles of your articles is that they are not URL friendly. They can contain spaces, UTF-8 characters and other stuff that will break the functionality of your web site.

And you want the title of the article to be in your URL because it is so much SEO and human friendlier. As I often develop sites in this way, here is a small but handy function that you can include in your code and call whenever you need to generate URL friendly text.

As I am often dealing with files, this function has an optional parameter called $dot which tells us whether to leave the dot inside the text (files needs that the dot is not removed) or not.

So here is the code:

function generateSlug($string, $space="-", $dot = null) {
        
        if (function_exists('iconv')) {
            $string = @iconv('UTF-8', 'ASCII//TRANSLIT', $string);
        }
        if (!$dot) {
            $string = preg_replace("/[^a-zA-Z0-9 \-]/", "", $string);
        } else {
            $string = preg_replace("/[^a-zA-Z0-9 \-\.]/", "", $string);
        }
        $string = trim(preg_replace("/\\s+/", " ", $string));
        $string = strtolower($string);
        $string = str_replace(" ", $space, $string);
        
        return $string;

    }

Basically, this function has 3 parameters: first is the text you want converted into URL friendly text, second is optional and tells the function which character should be used instead of spaces and the third is whether to leave the dots or remove them from text.

Function will first convert the input text into ASCII/TRANSLIT one using iconv function. This means that when a character can’t be represented in the target charset, it can be approximated through one or several similarly looking characters. For example, Croatian language has a letter Č which can not be represented in ASCII and it will be converted to C.

After that function is removing anything that is not alphanumerical from the string. Then, we removed extra (double) spaces, convert the string to lower case and filling the spaces with character in $space parameter (the default is -).

So if your title is:
How to generate URL friendly title
The result after this function will be:
how-to-generate-url-friendly-title

I hope that this small tip will come in handy.

Tweet about this on TwitterShare on FacebookShare on Google+Share on LinkedInShare on RedditShare on StumbleUpon



5 Responses to “How to generate URL friendly title”

Trackbacks/Pingbacks