Welcome to WebmasterWorld Guest from 54.196.217.43

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)

Full Member

10+ Year Member

joined:Mar 16, 2004
posts: 283
votes: 0


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)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 27, 2003
posts:732
votes: 0


$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)

Full Member

10+ Year Member

joined:Mar 16, 2004
posts: 283
votes: 0


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

Cheers, Pete

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

Senior Member from US 

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

joined:Apr 14, 2008
posts:2910
votes: 62


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