| Welcome to WebmasterWorld Guest from 22.214.171.124 |
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
|Subscribe to WebmasterWorld|
|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?
| 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.
| 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]
All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
© Webmaster World 1996-2014 all rights reserved