Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

URL Parameters Manipulation 1

Status
Not open for further replies.

Extension

Programmer
Nov 3, 2004
311
CA
Hi,

I trying to come up with an easy solution to change a specific parameter in the URL.
Example of URL format:
Code:
[URL unfurl="true"]http://www.domain.com/dir/script.php?id=343&view=y&lang=russian[/URL]

I want to be able to change the "lang" parameter.
FYI: The lang param is not always last in the URL.
I guess this could be handled with a Regex ?

Code:
$NEW_LANG = 'english';

$URL_STRING = $_SERVER['QUERY_STRING'];

... Regex to change lang value by $NEW_LANG ... 

$NEW_URL_STRING = $_SERVER['SCRIPT_NAME'] . "?" . $URL_STRING;
 
A weird sense of Dejavu....

$URL_STRING = str_replace('russian','english',$_SERVER['QUERY_STRING']);

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
No it's different this time.

The lang (language) in the URL is not always russian; close to 22 possibilities so a straight str_replace won't work.

 
If you mean to change multiple language tags to a single language description ("russian" and "german" and "french" will all go to "english") then a str_replace() with array input parameters likely will. See the second example on the PHP online manual page for str_replace()



Want the best answers? Ask the best questions! TANSTAAFL!
 
It could be
Code:
[URL unfurl="true"]http://www.domain.com/dir/script.php?id=343&view=y&lang=russian[/URL]
or
Code:
[URL unfurl="true"]http://www.domain.com/dir/script.php?id=343&view=y&lang=swedish[/URL]
etc..

I simply need a solution to change the lang value for another one.
 
Again, if you are changing any of a specific set of languages to "english", look at the second code example on the PHP online manual page for str_replace().

If you are trying to replace any language string in the URL:

[tt][ignore][/ignore][/tt]

to english:

[tt][ignore][/ignore][/tt]

Then you might have to resort to a regular expression.



Want the best answers? Ask the best questions! TANSTAAFL!
 
sleipnir214
Yes, this is what I'm trying to achieve. As you said, I guess a Regex will be the solution ?
 
If you are saying "yes" to the second scenario in my last post, yes, you are going to need a regular expression.

As I see it, there are only two cases you have to worry about, the case where the "lang" attribute is at the beginning of the sting of values and when it is not.

Play with this:

Code:
<?php
//$a = '[URL unfurl="true"]http://www.domain.com/dir/script.php?id=343&view=y&lang=swedish';[/URL]
//$a = '[URL unfurl="true"]http://www.domain.com/dir/script.php?lang=swedish&id=343&view=y';[/URL]
$a = '[URL unfurl="true"]http://www.domain.com/dir/script.php?id=343&lang=swedish&view=y';[/URL]
print preg_replace ('@([\?&])lang=[^\?&]*@', '$1lang=english', $a);

?>



Want the best answers? Ask the best questions! TANSTAAFL!
 

Thanks for your help Sleipnir214. Really appreciated.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top