/    Sign up×
Community /Pin to ProfileBookmark

Detect part of a URL

Hello,
I am working on a language feature.

For example, if a user is on “site.com/en/food/hamburger.php”, and they press the german icon, I need it to get the bit after the en, or de, and then take that information, and put it after the opposite (site.com/de)

Thanks

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@bionoidJan 14.2012 — Depending on how you get the current Url:

[code=php]<?php

$url = preg_replace('|^/[^/]+|', '/de', $_SERVER['REQUEST_URI']);

echo $url;

?>[/code]


With [B]$_SERVER['REQUEST_URI'][/B] I get [B]/en/food/hamburger.php[/B], then do the replacement as per above, and the result is [B]/de/food/hamburger.php[/B].

If you use/want a different method just say.
Copy linkTweet thisAlerts:
@leatesauthorJan 14.2012 — Where would I put this code?

Like this?

<img src="linktoimage" <phpcodehere>/> ?
Copy linkTweet thisAlerts:
@bionoidJan 14.2012 — I'm not sure what to suggest as I am uncertain of what you want.

Have you got something already written that needs looking at, or is this currently just a concept you want to try?
Copy linkTweet thisAlerts:
@leatesauthorJan 14.2012 — I need a button, (German flag icon) that when clicked, it links to the same file, just in the /de/ directory. So for example, if I'm on www.site.com/en/directory/page.php, and I click the German flag icon, it gets the part of the URL after /en/, and puts it after www.site.com/de/, and takes the user there.
Copy linkTweet thisAlerts:
@bionoidJan 14.2012 — Well, you could do it like this:

[code=php]<?php

echo
'<a href="' . preg_replace('|^/[^/]+|', ('/' . 'de'), $_SERVER['REQUEST_URI']) . '">' .
'<img src="[linktoimage]" alt="" />' .
'</a>';

?>[/code]


It would be nice to know where you're getting the current url from so I can do things properly ?
Copy linkTweet thisAlerts:
@leatesauthorJan 16.2012 — Brilliant, thanks!
×

Success!

Help @leates spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.27,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...