spacepoet Posted February 28, 2012 Share Posted February 28, 2012 Hello: I have this RegEx: ([a-zA-Z]+) I wanted to know how I can also add some code to it to convert all spaces to hyphens. So something like this: 12 Oz Bottle Will look like this: 12-Oz-Bottle Can someone help me ... please ... Quote Link to comment https://forums.phpfreaks.com/topic/257912-converting-spaces-to-hyphens/ Share on other sites More sharing options...
gizmola Posted February 28, 2012 Share Posted February 28, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/257912-converting-spaces-to-hyphens/#findComment-1321960 Share on other sites More sharing options...
Pikachu2000 Posted February 28, 2012 Share Posted February 28, 2012 You already have 2 other threads open related to this same problem. Quote Link to comment https://forums.phpfreaks.com/topic/257912-converting-spaces-to-hyphens/#findComment-1322022 Share on other sites More sharing options...
spacepoet Posted February 28, 2012 Author Share Posted February 28, 2012 I know ... i just cant find the answer ....its driving me nuts.... i know it has to do with the regex and adding the the spacrs to hyphens ... im not good with regex ... let me find another forum that deals with javascript ... Quote Link to comment https://forums.phpfreaks.com/topic/257912-converting-spaces-to-hyphens/#findComment-1322083 Share on other sites More sharing options...
joe92 Posted March 3, 2012 Share Posted March 3, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/257912-converting-spaces-to-hyphens/#findComment-1323345 Share on other sites More sharing options...
dannyb785 Posted March 3, 2012 Share Posted March 3, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/257912-converting-spaces-to-hyphens/#findComment-1323379 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.