How to use comma in URLs

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • AQuser

    How to use comma in URLs

    It's possible to use comma's in URLs for example: ?page=$page&id= 120

    Instead of AND (&) we use comma

    ?page=$page,id= 120
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    That should work fine. A comma is not a special char in URLs so it should come through as a part of the "page" value.

    Comment

    • AQuser

      #3
      Can you give me an example for this to replace & (and) with , (comma)

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        as far as I know, this needs your server to rewrite the url containing commas back into ampersands. in contrast to & (parameter separator), the comma does not have a special meaning. otherwise you’ll only get one URL parameter (page).

        Comment

        • kovik
          Recognized Expert Top Contributor
          • Jun 2007
          • 1044

          #5
          Or, you could convert it in PHP.

          Code:
          if (!empty($_GET) && count($_GET) == 1) {
          	$query_vars = array();
          	$query_data = explode(',', current($_GET));
          	
          	if (!empty($query_data)) {
          		$query_vars[key($_GET)] = array_shift($query_data);
          		
          		foreach ($query_data as $query_pair) {
          			if (!empty($query_pair)) {
          				$query_pair = explode('=', $query_pair, 2);
          				
          				if (count($query_pair) == 1) {
          					$query_vars[$query_pair[0]] = null;
          				} else {
          					$query_vars[$query_pair[0]] = $query_pair[1];
          				}
          			}
          		}
          	}
          	
          	$_GET = $query_vars;
          }
          Untested.

          Comment

          • kovik
            Recognized Expert Top Contributor
            • Jun 2007
            • 1044

            #6
            I found this idea interesting, and decided to write something a bit more concrete to solve your problem. I've explained it in more detail here.

            Code:
            /**
             * Rebuild the superglobal $_GET array
             *
             * @param string $query_string The query string
             * @param string $ampersand The replacement for ampersands in the query string
             * @param string $equality The replacement for equality signs in the query string
             * @return void
             */
            function rebuild_get($query_string, $ampersand = '&', $equality = '=') {
            	if (!empty($query_string)) {
            		// Empty the $_GET array
            		$_GET = array();
            		
            		// Insert key => value pairs into the $_GET array
            		foreach (explode($ampersand, $query_string) as $pair) {
            			$pair_data = explode($equality, $pair, 2);
            			$_GET[$pair_data[0]] = (count($pair_data) > 1) ? $pair_data[1] : null;
            		}
            	}
            }

            Comment

            • Markus
              Recognized Expert Expert
              • Jun 2007
              • 6092

              #7
              Why do you want to use a comma instead of an ampersand? AFAIK, the valid separators are ampersands (&) and semi-colons (;).

              Comment

              Working...