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

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
PeteM

10+ Year Member



 
Msg#: 4057757 posted 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:

"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

 

FourDegreez

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 4057757 posted 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.

PeteM

10+ Year Member



 
Msg#: 4057757 posted 6:23 pm on Jan 10, 2010 (gmt 0)

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

Cheers, Pete

TheMadScientist

WebmasterWorld Senior Member themadscientist us a WebmasterWorld Top Contributor of All Time 5+ Year Member



 
Msg#: 4057757 posted 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