Reputation: 45
I have an URL:
http://abv.com/
How can I check if /en/
is in the URL, for example:
http://abv.com/en/
Upvotes: 1
Views: 462
Reputation: 48006
You can take the URL and split it by slashes - use the .explode()
function.
$url = 'http://abv.com/en/';
$urlParts = explode('/',$url);
array_shift($urlParts);
array_shift($urlParts);
Using array_shift()
twice you remove the unwanted http
and the blank item due to the double slash...
Array
(
[0] => abv.com
[1] => en
[2] =>
)
.parse_url()
also has some usefull features for dealing with URL strings. You should check it out also.
$url = 'http://abv.com/en/';
$urlParts = parse_url($url);
$pathParts = explode('/',$urlParts['path']);
Upvotes: 1
Reputation: 5418
You can seperate url using php explode function, then check if url having "en" (country code ) or not.
$url = 'http://abv.com/en/';
$expurl = explode('/', $url);
print_r($expurl);
foreach ($expurl as $key => $value) {
if ($value == 'en') {
# do what you want
}
}
Array result
Array ( [0] => http: [1] => [2] => abv.com [3] => en [4] => )
Upvotes: 1
Reputation: 1557
You can use strpos()
.
$mystring = 'abc';
$findme = 'a';
$pos = strpos($mystring, $findme);
// The !== operator can also be used. Using != would not work as expected
// because the position of 'a' is 0. The statement (0 != false) evaluates
// to false.
if ($pos !== false) {
echo "The string '$findme' was found in the string '$mystring'";
echo " and exists at position $pos";
} else {
echo "The string '$findme' was not found in the string '$mystring'";
}
Upvotes: 2
Reputation: 9586
The simplest way to do that is using strpos()
:
if (strpos($url, '/en/') !== false) {
// found!
}
If you want to check just the path, though, using parse_url()
can be helpful:
if (strpos(parse_url($url, PHP_URL_PATH), '/en/') !== false) {
// found in the path!
}
Upvotes: 1