Welcome to WebmasterWorld Guest from 54.205.20.160

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Making Search Engine friendly URL's

   
1:44 pm on Jan 10, 2010 (gmt 0)

10+ Year Member



I'd like to stip superflous characters from a product name to create a key for my mod-rewrite script. So if my client creates a product called...

"blue items (pack of 12)"

It creates an key in the format:

"blue-items-pack-of-12"

...you'll see that all punctuation has been replaced by a hyphen. It's this bit that I don't know how to do in a single hit.

Any ideas how I go about this?

Thanks, Pete

5:54 pm on Jan 10, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$str = str_replace(' ', '-', trim(preg_replace('/([^a-z0-9]+)/i', ' ', $str)));

This will take any bits of the string that aren't alphanumeric characters and turn them into single spaces, then trim any leading or trailing spaces, then replace the spaces with hyphens.

The reason I didn't go straight to hyphens is because that last parenthesis would become a trailing hyphen.

6:23 pm on Jan 10, 2010 (gmt 0)

10+ Year Member



That's great, I'll give it a go.

Cheers, Pete

8:29 pm on Jan 10, 2010 (gmt 0)

WebmasterWorld Senior Member themadscientist is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



You'll probably need this one next:
Remove Extra Hyphens or Spaces from URL [webmasterworld.com]
 

Featured Threads

Hot Threads This Week

Hot Threads This Month