00 Votes

Create URL for Website from Title of Page

Tutorial by Stefan Trost | 2012-07-02 at 11:23

Search engine friendly URLs should not contain cryptic combinations of letters and numbers but rather relevant keywords wherever possible. A suitable possibility to realize this, is to generate the URL automatically from the title of the page.

In this tutorial, I will show you how to do that. Of course, the final function that we can see at the end of this text, can also be used to create URLs from other texts than the title, such as from the first characters of a paragraph, for example. 

Our Goals

First, we should agree on the goals of our project. The first thing we want to do is deleting potential spaces at the front or the back of the URL. For this we use:

$url = trim($title);

Next, we need to ensure that in the prospective URL, there are only lowercase characters. With this function, we can transform our input string to lowercase:

$url = mb_strtolower($title,'UTF-8');

Because otherwise, there may be problems with special characters and umlauts, we are using the multibyte function mb_strtolower instead of the function strtolower() and pass UTF-8 to the function as the encoding. Most new websites are encoded in UTF-8 today, so there is great chance, that this should also work for you. If you should use another encoding, you have to specify your different encoding here.

In our URL, we only want to have the lowercase letters a to z, the numbers 0 to 9 and the minus sign - as a separator. All other characters, we want to delete from the URL. But before we delete them, we should convert the characters, we do not want to lose in their equivalents. Exemplary, we are doing this with the German umlauts and ß, but again, here you can adjust the function according to your personal desires and the language the function should be designed for. In the same step, we are transforming spaces from the original string directly into the minus sign:

$url = str_replace(array(' ','ä','ö','ü','ß'),
       array('-','ae','oe','ue','ss'),$title);

We are using an array with all the letters, we want to convert and replace them with another array of the appropriate equivalents. For Example, we want to replace "ä" with "ae", "ß" with "ss", " " with "-" and so on. If you want to make any other substitutions, you can expand or shorten the array accordingly.

Now that we have saved all of the letters and special characters we want to keep, we can delete the rest of the characters that are not a lowercase letter from a to z, not a number and not a minus sign:

$url = preg_replace("([^a-z0-9/-])", "", $title);

The regular expression ([^a-z0-9/-]) searches for all characters that we do not want to have in our URL and replaces them with "", so that the characters are deleted.

Now we are almost done. What is missing is the final step:

$url = preg_replace("([/-]+)", "-", $title);

This line ensures that we do not have double, triple or even more minus signs after each other in our URL. With this regular expression, we are searching for all multiple occurrences of a minus sign and we are replacing these occurences with a single minus sign.

The final PHP Function

Now we have all lines of code that we need. Putting together the individual goals, results in the following function:

function CreateURL($text) {
   $url = mb_strtolower(trim($text, 'UTF-8'));
   $url = str_replace(array(' ','ä','ö','ü','ß'),
          array('-','ae','oe','ue','ss'), $url);
   $url = preg_replace("([^a-z0-9/-])", "", $url);
   return preg_replace("([/-]+)", "-", $url);
}

We can use our function like this:

$title = " Question: Is Anton taller than Jenny? ";
$url = CreateURL($title);   // "question-is-anton-taller-than-jenny"

Now, we can use this function to quickly and easily create URLs from any text.

ReplyPositiveNegative

About the Author

AvatarYou can find Software by Stefan Trost on sttmedia.com. Do you need an individual software solution according to your needs? - sttmedia.com/contact
Show Profile

 

Related Topics

HTACCESS: Simplify URL

Tutorial | 0 Comments

The Askingbox Search

Info | 0 Comments

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.