0

Possible Duplicate:
Replace values in a URI query string

I am using a language switcher in the navigation bar to switch between EN and DE, my url structure is like this:

http://www.mydomain.com/gallery.php?lang=de
http://www.mydomain.com/gallery-item.php?id=100&lang=de

The switch works well on all urls which don't have an id, but it doesn't work with the ids.

<?php
$url="http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
<a href='.$url.'?lang=en">
<a href='.$url.'?lang=de">
?>

What is a good solution to check for $lang=xx or ?lang=xx and then add ?lang=xx or $lang=xx respectively?

2
  • Are you using any htaccess code to rewrite the urls ? Commented Jun 29, 2012 at 8:25
  • Try to add the ? only, if $_SERVER['PHP_SELF'] is not set (this variable contains the url parameters) and add an & if it is set. :) Commented Jun 29, 2012 at 8:25

1 Answer 1

0
$params = $_GET;

$params['lang'] = 'en';
printf('<a href="%s?%s">En</a>', $url, http_build_query($params));

$params['lang'] = 'de';
printf('<a href="%s?%s">De</a>', $url, http_build_query($params));

Or even very compact:

printf('<a href="%s?%s">De</a>',
       $url, http_build_query(array('lang' => 'de') + $_GET));

http://php.net/http_build_query

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.