Mauricen Posted March 12, 2013 Share Posted March 12, 2013 Hi, PHP geniuses. I am a PHP novice and am stuck. For someone knowledgeable, this should be pretty easy, but it is stumping me. I have a site that is multiple languages. The only difference in the pages from one language to the next is a "/it/" at the end of the main url before the page name. I need 2 inline PHP statements that create hrefs: One that creates a link that says "remove 'it/' from the current page URL One that takes the current url and ads a 'it/" after the domain name and before the page name in a link. Example: If the current page URL is http://www.xxx.com/it/test/ - it creates a link that is http://www.xxx.com/test/ The second piece of code creates a link that changes http://www.xxx.com/test/ to http://www.xxx.com/it/test/ I have played with some PHP string replace commands, but I can't get it to work. I need something like: <a href='<?php echo str_replace ("/it/",""; ?>'> Anyway, bad code example, but you get the idea. Anyone know a quick fix here? Link to comment https://forums.phpfreaks.com/topic/275556-addingremoving-string-from-a-url/ Share on other sites More sharing options...
requinix Posted March 12, 2013 Share Posted March 12, 2013 I need something like: But don't you need that "it"? It tells you the language... Link to comment https://forums.phpfreaks.com/topic/275556-addingremoving-string-from-a-url/#findComment-1418220 Share on other sites More sharing options...
yomanny Posted March 12, 2013 Share Posted March 12, 2013 // Example #1 $url = 'http://www.test.com/it/test/'; $new_url = str_replace('/it/', '/', $url); echo $new_url; // Example #2 $url = 'http://www.test.com/test/'; $new_url = str_replace('test.com/', 'test.com/it/', $url); echo $new_url; This, however, relies on the fact that you know the domain name (test.com in these examples). If you want it more dynamic, take a look at Regular Expressions, you can do a lot of interesting text manipulation stuff with it! - W Link to comment https://forums.phpfreaks.com/topic/275556-addingremoving-string-from-a-url/#findComment-1418239 Share on other sites More sharing options...
Mauricen Posted March 12, 2013 Author Share Posted March 12, 2013 Yomanny... that looks like what I need to replace the code. But how do I 'on the fly' pull the current URL... and how do I embed the <?php echo $new_url ?> in line with the HTML href quotes? <a href="<?php echo $new_url ?>">Italian</a> - that syntax doesn't work. Link to comment https://forums.phpfreaks.com/topic/275556-addingremoving-string-from-a-url/#findComment-1418253 Share on other sites More sharing options...
Mauricen Posted March 12, 2013 Author Share Posted March 12, 2013 O.K., it with your help and a little more research, I figured this out! <?php $domain = $_SERVER['HTTP_HOST']; $queryString = $_SERVER['QUERY_STRING']; $url = "http://" . $domain . $_SERVER['REQUEST_URI']; $new_url = str_replace('test.com/', 'test.com/it/', $url);?> <a href="<?php echo $new_url;?>">Italiano</a> It works beautifully! Thank you YoManny for your help! Link to comment https://forums.phpfreaks.com/topic/275556-addingremoving-string-from-a-url/#findComment-1418266 Share on other sites More sharing options...
yomanny Posted March 13, 2013 Share Posted March 13, 2013 Glad it helped! - W Link to comment https://forums.phpfreaks.com/topic/275556-addingremoving-string-from-a-url/#findComment-1418315 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.