Create URL Slug from Post Title

/ Published in: PHP
Save to your folder(s)

Copy this code and paste it in your HTML
  1. Regular expression function that replaces spaces between words with hyphens.
  3. <?php
  4. function create_slug($string){
  5. $slug=preg_replace('/[^A-Za-z0-9-]+/', '-', $string);
  6. return $slug;
  7. }
  8. echo create_slug('does this thing work or not');
  9. //returns 'does-this-thing-work-or-not'
  10. ?>


Report this snippet


RSS Icon Subscribe to comments

You need to login to post a comment.