Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Jun 2007
    Location
    Maryland, USA
    Posts
    165
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Passing a variable with an & through $_GET

    I'm having some trouble passing a variable through $_GET.

    The variable is
    Code:
    $category = "Business & Entrepreneurial";
    It needs to be passed from one page to another using $_GET.

    However, because the value has an ampersand in it, only the portion of the value before the ampersand is transferred.

    So, for "Business & Entrepreneurial", the value of $category is transferred simply as "Business ".

    I have to pass along a few values with this sort of misleading punctuation, including ampersands forward slashes.

    I know that & is the html code for and ampersand, but it won't help much to use that since it actually has an ampersand in it and creates the same problem.

    Does anyone know of a fix or work around?

    Thanks.

  • #2
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,778
    Thanks
    19
    Thanked 155 Times in 146 Posts
    Change the ampersand to "%26", or use urlencode():

    PHP Code:
    $category urlencode("Business & Entrepreneurial"); 
    Regards, R.J.

    ---------------------------------------------------------

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!

  • Users who have thanked chump2877 for this post:

    madmatter23 (09-05-2007)

  • #3
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts

  • #4
    Regular Coder
    Join Date
    Jun 2007
    Location
    Maryland, USA
    Posts
    165
    Thanks
    12
    Thanked 0 Times in 0 Posts

    The next level

    Thanks so much for the quick responses.

    I love finding out that the makers of php have created a function for exactly what I need. However, the problem is actually a bit more complicated than I let on.

    I'm actually using mod_rewrite to pass $_GET variables. (just when I thought I was done with all of those annoying .htaccess problems).

    The rewrite rule is this:
    Code:
    RewriteRule browse/(.+)/?$ /browsecategory.php?category=$1 [L,NC,NE]
    Entering the real URL (as encoded by urlencode) into the browser works fine, so:
    http://browsecategory.php?category=B...ntreprenuerial
    is all good.

    However, typing
    browse/Business+%26amp%3B+Entreprenuerial
    causes the value of $_GET["category"] to set only as "Business"

    The rewrite rule does work for categories with no punctuation.

    Apparently the rule is being thrown off by the urlencoding, much like the $_GET array was thrown off by an ampersand.

    Ugh. Anyone have any ideas?

  • #5
    Regular Coder
    Join Date
    Jun 2007
    Location
    Maryland, USA
    Posts
    165
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Got it.

    Found a tip online that said to use urlencode twice. For some reason this works.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •