PHP Function to Create Nice URLs

This function will allow you to create nice friendly SEO URLs with PHP using prep_replace function. A useful function for blog post titles.

I have always liked nice urls in website it makes it cleaner and gives you better result for SEO. This is why i like to share this function on making nice URLs. You can store the title of your page/blog/etc in this format and then call it in your query. 

Here is an example of what the function is capable of. 

Title: This is a Great Blog Post

Response: this-is-a-great-blog-post

So here is the code that you will need

$name = 'This is the best blog post ever';

//SEO URL
$name_url = str_replace(' ', '-', strtolower($name));
$name_url = preg_replace('~[^a-z0-9_-]+~i', '', $name_url);

//response 
//this-is-the-best-blog-post-ever
echo $name_url;