Novocaine88 Posted April 18, 2011 Share Posted April 18, 2011 Hi, I've got a little problem at the moment with how to replace varying amounts of hyphens in a browser url. scenario: I have a borwser url mydomain.com/location/foo-bar/ where "foo-bar" is actually "foo bar" in the database (no hyphen). I also have urls where the entry in the database does have the hyphens e.g. "foo-bar" Now im not quite sure how to go about writing these urls, and how to manipulate the GET values from them. what I've tried My first thought was to add a secondary hyphen to the url so that the database entires that equal "foo-bar" would be "foo--bar" in the browser url and the entries in the database that equal "foo bar" would have the single hyphen "foo-bar". Then i would use preg_replace to deal with the single hyphens by removing them and replacing with a space, and after that finding the double hyphens and replacing them with only one. In theory this should work (I think), but I've only recently got into using regex, this is what I've tried; $CNE = preg_replace("/[-]{1}/", " ", $CNE); $CNE = preg_replace("/[-]{2}/", "-", $CNE); so I've tired to match the single hyphen and then the double, but even before I tested this I didn't think it was going to work with this code. Can anyone correct me, or maybe suggest a better way to go about this issue. (note: using a different character to replace spaces is NOT an option in this case) Thanks Quote Link to comment https://forums.phpfreaks.com/topic/234046-preg_replace-hyphens/ Share on other sites More sharing options...
Nodral Posted April 18, 2011 Share Posted April 18, 2011 Hi You could try using the rawurlencode() & rawurldecode() functions. Surely this would be a better way forward? I'm new to php myself so apologies if this isn't 100% correct, but it's just another train of thought for you. $input=$_GET['input']; $CNE = rawurlencode($input); Quote Link to comment https://forums.phpfreaks.com/topic/234046-preg_replace-hyphens/#findComment-1202956 Share on other sites More sharing options...
Novocaine88 Posted April 18, 2011 Author Share Posted April 18, 2011 Hi You could try using the rawurlencode() & rawurldecode() functions. Surely this would be a better way forward? I'm new to php myself so apologies if this isn't 100% correct, but it's just another train of thought for you. $input=$_GET['input']; $CNE = rawurlencode($input); Thanks for the reply, however I'm not sure how you mean me to use that? the url as it is, mydomain.com/location/foo-bar/ the get value of that will still be foo-bar after using rawurlencode(). I basically need a way to replace a single hyphen with a space, and if there are 2 hyphens in a row replace them with a single instance. I would of thought regex would be the best option for this but like i said, i've little experience with them. Although I said I don't want to change the character that represent a space in teh browser url, if its easier and underscore might be acceptable. e.g. "foo_bar" to represent 2 separate words whilst "foo-bar" would represent one after getting it from the url. Quote Link to comment https://forums.phpfreaks.com/topic/234046-preg_replace-hyphens/#findComment-1202976 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.