Page 1 of 1
Ampersand and $_GET
Posted: Sat Jan 27, 2007 6:15 pm
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.
Posted: Sat Jan 27, 2007 6:26 pm
by superdezign
Nevermind, he has informed me that simply replacing it with the word "And" is fine.
Posted: Sat Jan 27, 2007 6:37 pm
by mikesmith76
Just for future reference, wouldn't
urlencode do the job?
Posted: Sat Jan 27, 2007 7:46 pm
by Ambush Commander
Yes. & is for guarding the ampersand against special meaning in HTML, while urlencoding it guards it against special meaning in URIs.
Posted: Sun Jan 28, 2007 12:36 am
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)
Posted: Sun Jan 28, 2007 3:48 am
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;
}
Posted: Sun Jan 28, 2007 3:55 am
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
Posted: Sun Jan 28, 2007 4:03 am
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
Posted: Sun Jan 28, 2007 4:19 am
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.