user2997418
user2997418

Reputation:

Detect language in my url

I have a url and I want to detect the language to save it in the cookies. This is my url :

https://www.example.com/en/test.php

Upvotes: 1

Views: 4802

Answers (2)

user6696028
user6696028

Reputation:

Get the current url (without serveur) :

$url = $_SERVER['REQUEST_URI'];   // gives "/en/test.php"

Then explode it in an array :

$urlParts = explode ('/', $url); // split the url by /

The first element (index 0) is empty (because the string starts with /), and the language is the second one (index 1)

$language = $urlParts[1] ;

Don't forget to check at each steps if the url is OK and the arrayx contains more than 1 element.

Upvotes: 1

Kristiyan
Kristiyan

Reputation: 1663

There are different methods to do it:

1. Method A (explode REQUEST_URI):

echo $_SERVER['REQUEST_URI']; // /en/test.php
$exp = explode('/',$_SERVER['REQUEST_URI']); // explode by slash
$language = $exp[1]; // first element before / (slash)

Upvotes: 2

Related Questions