Skip to content

Instantly share code, notes, and snippets.

@webmechanicx
Created February 29, 2016 23:10
Show Gist options
  • Save webmechanicx/6fc2e04f59af46c7c7d7 to your computer and use it in GitHub Desktop.
Save webmechanicx/6fc2e04f59af46c7c7d7 to your computer and use it in GitHub Desktop.
To covert a string to SEO friendly Slug-URL
<?php
/* Sluggify - Simple Logic to turn a string to slug url
* To covert a string to SEO friendly
*/
$title = "To covert a string to SEO friendly"
$slugs = preg_replace('/\%/',' percentage',$title);
$slugs = preg_replace('/\@/',' at ',$slugs);
$slugs = preg_replace('/\&/',' and ',$slugs);
$slugs = preg_replace('/\s[\s]+/','-',$slugs); // Strip off multiple spaces
$slugs = preg_replace('/[\s\W]+/','-',$slugs); // Strip off spaces and non-alpha-numeric
$slugs = preg_replace('/^[\-]+/','',$slugs); // Strip off the starting hyphens
$slugs = preg_replace('/[\-]+$/','',$slugs); // Strip off the ending hyphens
$slugs = strtolower($slugs);
echo $slugs;
//output: this-is-the-string-to-be-made-seo-friendly
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment