Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

PHP detect if slug URL starts with ES/ or EN/

I need to detect the language the user is using to include the correct file using PHP if elseif or else like this:

users are comming from:

example.com/EN/nice-title-url-from-database-slug
example.com/DE/nice-title-url-from-database-slug
example.com/ES/nice-title-url-from-database-slug

the php I need is something like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

PHP
document.location.toString().split(…) etc
detect the url paths

if url path <starts with /DE/>
  include de.php
elseif url <path starts with /EN/>
  include en.php
else url <path starts with /ES/>
  include es.php

so what I need is to detect the url after the domain (/ES/ or /EN/ or /DE/)

Any idea how to achieve this?

>Solution :

To achieve what we want, we need to:

  1. Find the URL of the current page – we can use $_SERVER[‘REQUEST_URI’] (Get the full URL in PHP
  2. From this, we want to figure out if it contains the language part. One way to do this, is to split the string, as you kindly suggest, and get second result (which is key 1): explode(‘/’, $_SERVER[‘REQUEST_URI’])1
  3. Then we can do the include or what logic you need.

So following would be my suggestion.

// Get the uri of the request.
$uri = $_SERVER['REQUEST_URI'];

// Split it to get the language part.
$lang = explode('/', $uri)[1]; // 1 is the key - if the language part is placed different, this should be changed.

// Do our logic.
if ($lang === 'DE') {
  include 'de.php';
} else if ($lang === 'EN') {
  include 'en.php';
} else if ($lang === 'ES') {
  include 'es.php';
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading