Search code examples
phpregexstringslug

php/regex: "linkify" blog titles


I'm trying to write a simple PHP function that can take a string like

Topic: Some stuff, Maybe some more, it's my stuff?

and return

topic-some-stuff-maybe-some-more-its-my-stuff

As such:

  • lowercase
  • remove all non-alphanumeric non-space characters
  • replace all spaces (or groups of spaces) with hyphens

Can I do this with a single regex?


Solution

  • Many frameworks provide functions for this

    CodeIgniter: http://bitbucket.org/ellislab/codeigniter/src/c39315f13a76/system/helpers/url_helper.php#cl-472

    wordpress (has many more in the code): http://core.trac.wordpress.org/browser/trunk/wp-includes/formatting.php#L814