drupal: standard way for creating a slug from a string

Posted by egarcia on Stack Overflow See other posts from Stack Overflow or by egarcia
Published on 2010-05-19T10:48:37Z Indexed on 2010/05/19 10:50 UTC
Read the original article Hit count: 268

Filed under:
|
|
|

Hi there,

A slug on this context is a string that its safe to use as an identifier, on urls or css. For example, if you have this string:

I'd like to eat at McRunchies!

Its slug would be:

i-d-like-to-eat-at-mcrunchies-

I want to know whether there's a standard way of building such strings on Drupal (or php functions available from drupal). More precisely, inside a Drupal theme.

Context: I'm modifying a drupal theme so the html of the nodes it generates include their taxonomy terms as css classes on their containing div. Trouble is, some of those terms' names aren't valid css class names. I need to "slugify" the them.

I've read that some people simply do this:

str_replace(" ", "-", $term->name)

This isn't really a enough for me. It doesn't replace uppercase letters with downcase, but more importantly, doesn't replace non-ascii characters (like à or é) by their ascii equivalents.

Is there a function in drupal 6 (or the php libs) that provides a way to slugify a string, and can be used on a template.php file of a drupal theme?

© Stack Overflow or respective owner

Related posts about drupal

Related posts about php