PHP- Create slug from string

Below is simple function to slug from string.

Code

1
2
3
4
5
6
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'

Output

1
does-this-thing-work-or-not

No comments:

Post a Comment