Detect language in my url - php

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

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)

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.

Related

Remove part URL before second last slash in PHP

How can I remove the part in my URL that is before the second last slash?
My URL is this:
http://antwerp.test/nl/aanbod/activiteiten/kunst/the-world-of-steve-mccurry/the-world-of-steve-mccurry/activity-mccurry/
So I want to remove "the-world-of-steve-mccurry/" the second time it appears. The URL should be:
http://antwerp.test/nl/aanbod/activiteiten/kunst/the-world-of-steve-mccurry/activity-mccurry/
you can explode the url by "/" , then remove the mentioned part , then implode again :
function sms($url)
{
substr($url,-1) === "/"? $position = 3:$position = 2; // decide if URL ends with `/` or not
$partsArray = explode('/',$url);
$partsArrayCount = count($partsArray);
$toDelete = $partsArray[$partsArrayCount-$position]; // get the part that you want to delete
unset($partsArray[$partsArrayCount-3]);
$result = implode("/",$partsArray);
return $result;
}

Check if URL has content in PHP

I've a PHP file. In this file I need to check, if my URL has the following ending:
www.example.de/dashboard/2/
So the ending can be a number 1 - 99+ which is always at the end of the url between two slashes. I can't use $_GET here. If it is $_GET, it would be easy:
if ( isset($_GET['ending']) ) :
So how can I do this without a parameter in the URL? Thanks for your help!
if(preg_match('^\/dashboard\/(\d+)', $_SERVER['REQUEST_URI'])){
foo();
}
Use regular expression on the request uri
You can make use of parse_url and explode:
$url = 'http://www.example.de/dashboard/2/';
$path = parse_url($url, PHP_URL_PATH); // '/dashboard/2/'
$parts = explode('/', $path); // ['', 'dashboard', '2', '']
$section = $parts[1]; // 'dashboard'
$ending = $parts[2]; // '2'
Demo: https://3v4l.org/dv6Cn
You can also make use of URL rewriting (this is for a Apache-based web server, but you can find simular resources for nginx or any other web servers if need be).
A more dynamic way is to explode and use array_filter to remove empty values then pick the last item.
If the item * 1 is the same as the item then we know it's a number.
(The return from explode is strings so we cant use is_int)
$url = "http://www.example.de/dashboard/2/";
$parts = array_filter(explode("/", $url));
$ending = end($parts);
if($ending*1 == $ending) echo $ending; //2
First you need to target this url to script - in web server config. For nginx and index.php:
try_files $uri #rewrite_location;
location #rewrite_location {
rewrite ^/(.*) /index.php?link=$1&$args last;
}
Second - you need to parse URI. In $end you find what you want
$link_as_array = array_values(array_diff(explode("/", $url), array('')));
$max = count($link_as_array) - 1;
$end = $link_as_array[$max];
I would think this way. If the URL is always the same, or the same format, I'll do the following:
Check for the approx URL.
Split the URL into pieces.
Find if there's the part I am looking for.
Extract the number.
<?php
$url = "http://www.example.de/dashboard/2/";
if (strpos($url, "www.example.de/dashboard") === 7 or strpos($url, "www.example.de/dashboard") === 8) {
$urlParts = explode("/", $url);
if (isset($urlParts[4]) && isNumeric($urlParts[4]))
echo "Yes! It is {$urlParts[4]}.";
}
?>
The strpos with 7 and 8 is for URL with http:// or https://.
The above will give you the output as the numeric part if it is set. I hope this works out.

Remove only 3 characters after a specific string

Hi I have this string for example
http://aaa-aaaa.com/bbbb-bbbbbbbbbb-2/it/clients/
I want to remove only 3 characters after bbbb-bbbbbbbbbb-2/, so basically I want to remove the it/ part (This it/ part may not always be it but it can be es/ or en/ or different languages always 2 characters )
The following will work provided the the URL structure doesn't change. I assume you're wanting to remove the language part of the URL.
<?php
$url = "http://aaa-aaaa.com/bbbb-bbbbbbbbbb-2/it/clients/";
$parsedURL = parse_url($url);
$path = explode('/', $parsedURL['path']);
unset($path[2]);
$url = "{$parsedURL['scheme']}://{$parsedURL['host']}";
$url .= implode('/', $path);
var_dump($url);
// string(47) "http://aaa-aaaa.com/bbbb-bbbbbbbbbb-2/clients/"
You can use regex to selecting target part of string and run it in preg_replace().
$url = "http://aaa-aaaa.com/bbbb-bbbbbbbbbb-2/it/clients/";
echo preg_replace("#(.*)\w{2}/([^/]+/)$#", "$1$2", $url);
See result of code in demo
Define your languages in an array. If a language is not defined in the array URL will be the same.
$mLanguages = ["en","it","bg","gr"];
$mURL = "http://aaa-aaaa.com/bbbb-bbbbbbbbbb-2/it/clients/";
$mURL = removeLanguageFromURL($mURL, $mLanguages);
echo $mURL; // Output http://aaa-aaaa.com/bbbb-bbbbbbbbbb-2/clients/
function removeLanguageFromUrl($mURL, $mLanguages){
foreach($mLanguages as $language){ // Search languages
if(strpos($mURL, $language) !== false) // If language is found in url remove it
$mURL = str_replace('/' . $language,'', $mURL);
}
return $mURL;
}

php count wordand remove

how to remove some word on url inp php?
URL : /hiaz/0.1.1/index.php?route=product/product&product_id=153&tracking=507b790fadc48
i just want :
tracking=507b790fadc48
If you have the full url stored in the var $myurl:
$result = substr($myurl, strrpos($myurl, "tracking"), strlen ($myurl) );
You can make use of $_GET if you are working in the index.php. Alternatively, if you wish to grab whole query string, you can use $_SERVER['QUERY_STRING']
What you have is
$url = /hiaz/0.1.1/index.php?route=product/product&product_id=153&tracking=507b790fadc48
What you want is
$newurl = /hiaz/0.1.1/index.php?tracking=507b790fadc48
First extract part of url upto "?" character.
Then extract the tracking part and combine.
$part1 = substr($url,0,strpos($url,'?'));
$part2 = substr($url,strpos($url,"tracking"),strlen($url)-strpos($url,"tracking"));
$newurl = $part1 + $part2;

php : Retrieving number in the URL

I have url in the following types
http://domain.com/1/index.php
http://domain.com/2/index.php
http://domain.com/3/index.php
http://domain.com/4/index.php
I need to retrieve only the number from the url .
for example,
when i visit http://domain.com/1/index.php , It must return 1.
Have a look at parse_url.
$url = parse_url('http://domain.com/1/index.php');
EDIT: Take a look at $_SERVER['REQUEST_URI'], to get the current URL. Use that instead of $url['path'].
Then you can split $url['path'] on /, and get the 1st element.
// use trim to remove the starting slash in 'path'
$path = explode('/', trim($url['path'], '/'));
$id = $path[0]; // 1
Given the information provided, this will do what you ask... it's not what I would call a robust solution:
$url = $_SERVER['PATH_INFO']; // e.g.: "http://domain.com/1/index.php";
$pieces = explode("/", $url);
$num = $pieces[3];
split the server path by forward slashes (explode('/', $_SERVER['REQUEST_PATH']);)
remove empty entry from the beginning
take the first element
make sure it is an integer (intval(), or a simple (int) cast).
No need to use regular expressions for this.
You use preg_match() to match the domain and get the 1st segment.
$domain = http://www.domain.com/1/test.html
preg_match("/http:\/\/.*\/(.*)\/.*/", "http://www.domain.com/1/test.html");
echo $matches[1]; // returns the number 1 in this example.

Categories