Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Adam Menczykowski
2,319 PointsAdd subdomain to current domain
Hi guys,
I have created a welsh version of an originally English website.
English: xperiorfarmhealth.co.uk Welsh: welsh.xperiorfarmhealth.co.uk
What I want to do is link on every page in the header to the corresponding welsh version.
I have managed to parse the current url ( http://xperiorfarmhealth.co.uk/vets/ ) into a string....
$current_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$link_array = parse_url($current_link);
var_dump ($link_array);
Which outputs:
array(3) { ["scheme"]=> string(4) "http" ["host"]=> string(12) "xperiorfarmhealth.co.uk" ["path"]=> string(6) "/vets/" }
How can I add "welsh" after the scheme and before the host, then turn it back into a url? I want the result to be: http://welsh.xperiorfarmhealth.co.uk/vets/
Also I want to do the exact reverse of that, turning http://welsh.xperiorfarmhealth.co.uk/vets/ into http://xperiorfarmhealth.co.uk/vets/
Thanks in advance guys Adam
6 Answers
Michael Collins
433 Points$current_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; $link_array = parse_url($current_link);
we need to prepend "welsh" into the $link_array['host'];
$link_array['host'] = 'welsh.' . $link_array['host'];
here we concatenate the various parts to make the new URL link
$new_link = $link_array['scheme'] . '://' . $link_array['host'] . $link_array['path'];
Adam Menczykowski
2,319 PointsAwesome, I'll try it out. Thanks so much
Adam Menczykowski
2,319 PointsAlso, how can I do the reverse of that process, removing welsh, so that I can link to the corresponding english page?
Adam Menczykowski
2,319 PointsAlso, how can I do the reverse of that process, removing welsh, so that I can link to the corresponding english page?
Michael Collins
433 PointsSo assuming that $link_array['host'] is already populated with with a value beginning with "welsh."
$link_array['host'] = preg_replace('/^welsh\./', '', $link_array['host']);
The carrot (^) means that the pattern "welsh." must be found at the beginning to match. The (.) means that the period should be treated as literal period. Without the backslash, the period is a meta character which means ANY character.
preg_replace takes $link_array['host'] as the subject. It then looks for the pattern (welsh.) only at the beginning. If it finds the pattern, it replaces it with nothing ('').
Adam Menczykowski
2,319 PointsThank you so much, and thank you for the extra explanation in addition to the solution.
Noah Laurent
3,080 PointsNoah Laurent
3,080 PointsHello Adam, did you use WordPress? WordPress Multisite? Thanks, Laurent