Skip to main content
Home / Code Snippets / PHP / Create URL Slug from Post Title

Create URL Slug from Post Title

Regular expression function that replaces spaces between words with hyphens.

<?php
function create_slug($string){
   $slug=preg_replace('/[^A-Za-z0-9-]+/', '-', $string);
   return $slug;
}
echo create_slug('does this thing work or not');
//returns 'does-this-thing-work-or-not'
?>
icon-link icon-logo-star icon-search icon-star