Jump to content

Converting Spaces to hyphens


spacepoet

Recommended Posts

Without more context it's hard to provide you an answer.  Regex is designed for pattern matching, and the pattern you presented will not match spaces.  More than that it will also not match any digits.  You could make an allowance for spaces and digits, but in order to use one of the "search + replace" functions, you really need to be matching all your spaces, and I'm guessing the point of the regex is not to try and break things down to the individual character which would be essentially useless.  You can do search and replace with specific regex functions, but this involves capturing portions of the match, and telling the regex function what to substitute for those captured sections in a second parameter that is seperate from the regex that does the matching/capturing.

 

Regex is powerful, but not fast, so you shouldn't use regex for problems that can be solved without it.  In your example, I'd suggest you use str_replace to change your spaces to dashes. 

 

 

 

 

 

 

Link to comment
Share on other sites

Are you trying to match for the '12 Oz Bottle' and replace the spaces at the same time? If so, this thread that I created a while ago will give you a huge helping hand. If not - and you already have the string, you just need to replace the spaces into underscores - then gizmola already solved that one for you :)

 

Joe

Link to comment
Share on other sites

maybe I'm missing something, why hasn't str_replace been used? Seems to me thatd be the easiest. Are you trying to convert strings to url-friendly?

 

Or are you trying to find all instances of "XX oz bottle" to convert? If that's the case, a simple str_replace(" oz bottle", "-oz-bottle", $string) is all you'd need.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.