Jump to content

preg_replace hyphens


Novocaine88

Recommended Posts

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

 

 

 

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

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.

 

 

 

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.