I created a blog and want to replace part of the URL, when an article is opened, by the title of the article.
I want to replace the part a.php?a=3&v=0
with the title, it would be
The system you want is called slug
.
To work with Slug, it's easy, but it depends on some settings, such as URL amigável
.
In this example, I'm creating a function that gets the title of the article and converts it to slug
<?php
function slug($title){
$slug = preg_replace('/[^A-Za-z0-9-]+/', '-', $title);
return $slug;
}
echo slug('O mundo vai acabar em 2016'); //O-mundo-vai-acabar-em-2016'
?>
The function is very basic, you can add other functions: strtolower
or improve pattern
to avoid special characters.
The must function be used in the link, which will call the page. I recommend that you add before the slug, a parameter for ID
, such as "site.com.br/1293/o-world-windows-in-2016"
The url will be theoretically clean for both the search platforms and the end user.
Unless you use the ID
parameter before the slug, you should have some attentions