homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

Making Search Engine friendly URL's

 1:44 pm on Jan 10, 2010 (gmt 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:


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

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

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

Cheers, Pete


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

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

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved