We Recommend

Wicked Cool PHP: Real-World Scripts That Solve Difficult Problems Wicked Cool PHP: Real-World Scripts That Solve Difficult Problems
Wicked Cool PHP contains a wide variety of scripts to process credit cards, check the validity of email addresses, template HTML, and serve dynamic images and text.


Posted By

tylerhall on 06/06/07


Tagged

php textmate String slug


Versions (?)


Who likes this?

8 people have marked this snippet as a favorite

samuraicoder
damarev
imbuez
basicmagic
n00ge
chucktrukk
romanos
tikitakfire


Convert String to Slug


Published in: PHP 


This filters a string into a "friendly" string for use in URL's. It converts the string to lower case and replaces any non-alphanumeric (and accented) characters with dashes.

  1. function slug($str)
  2. {
  3. $str = strtolower(trim($str));
  4. $str = preg_replace('/[^a-z0-9-]/', '-', $str);
  5. $str = preg_replace('/-+/', "-", $str);
  6. return $str;
  7. }

Report this snippet 

You need to login to post a comment.