Ampersand and $_GET

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Ampersand and $_GET

Post by superdezign »

Well I'm a bit stuck.

I've got it set up so that my client can list a bunch of clothing designers that they carry, and a few of them have an ampersand in their name. i.e. Dolce & Gabanna or J & Co.

This causes a problem in sending these variables through the URL, but it's been serving me so well

Is there a way to make the URL count it as a character rather than a separator?


I'm considering a forced preg_replace changing & to "amp;", but I'm hoping there's a more efficient solution.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Nevermind, he has informed me that simply replacing it with the word "And" is fine.
mikesmith76
Forum Commoner
Posts: 34
Joined: Fri Aug 25, 2006 7:10 am
Location: Manchester, UK

Post by mikesmith76 »

Just for future reference, wouldn't urlencode do the job?
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Yes. & is for guarding the ampersand against special meaning in HTML, while urlencoding it guards it against special meaning in URIs.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Well that makes perfect sense. Now I've gotta know.. How would I implement that via regex?

Please note that I just recently began to delve into the power of regex, but I've gotta know.. how do you use a function in it?

i.e. preg_replace('/action=([^\"&]+)/', 'action='.urlencode($1), $hrefUrl)
User avatar
louie35
Forum Contributor
Posts: 144
Joined: Fri Jan 26, 2007 8:40 am
Location: Dublin
Contact:

Post by louie35 »

& passed into a query string will go back to & but it will show in the source code as "&".
I have had a similar problem so I used str_replace to do the job:

Code: Select all

$str = str_replace("&", "_", $str);

//or

$str = str_replace("&", "and", $str);

//you can have this into a function so you can use it when needed
function clean_url($str){
  $str = str_replace("&", "_", $str);

  //or

  $str = str_replace("&", "and", $str);

  return $str;
}
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Yeah, that's precisely my solution; replace the ampersands with "and." I was thinking it was possible to pass it through as an ampersand and use $_GET to get it as an ampersand after the send.

At the moment, I'm sending it through as "and" and displaying it as an ampersand. I really feel as though regex should have a method to using functions to format strings within
User avatar
louie35
Forum Contributor
Posts: 144
Joined: Fri Jan 26, 2007 8:40 am
Location: Dublin
Contact:

Post by louie35 »

yes the is but you can not use it on the entire query string cause it will replace all the & and will trow an error

Is this what you are after:

Code: Select all

$str = preg_replace ( "/\&/", "and", $str );//haven't test it
did you look into RegExLib website: http://regexlib.com/default.aspx
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Yes, however, that's exactly what I'm currently doing. What I would like to do is pass the ampersand through using the urlencode() function

Edit: No matter. What I have will suffice.
Post Reply