You can simply use regex, as long as the site URL is "fixed", doing something like:
<?php
$url = 'https://www.localhost.com/desconto/nome-da-loja/';
if (preg_match('#^https?:\/\/www\.localhost\.com\/desconto\/([^\/]+)\/#', $url, $matches)) {
$loja = $matches[1];
var_dump($loja);
} else {
echo 'URL invalida';
}
Remarking, as long as the URL "is fixed" and only the store name changes.
The regex is simple, each point is necessary to escape because the .
is used as a kind of wildcard
The ([^\/]+)
is a group, this group takes anything that does not contain /
, the sign ^
indicates that it has to be different than the one inside [...]
and the sign of +
at the end of the group indicates that have to take everything until the next match of the regex (the next part of the regex is \/
, before the #
)
The #
php is used to delimit only, it has nothing to do with the regex used itself.
If you’re using with .htaccess
can pick up through the REQUEST_URI
, doing something like (simpler):
<?php
$url = $_SERVER['REQUEST_URI'];
if (preg_match('#^/desconto\/([^\/]+)\/#', $url, $matches)) {
$loja = $matches[1];
var_dump($loja);
} else {
echo 'URL invalida';
}
Read about the function
parse_url
.– Woss
Why don’t you pass by
get
? creates the link to the address "https://www.localhost.com/desconto.php?nome_loja=nome-da-loja
" and then recovers using $_GET['store name']. I know it’s not the best alternative, but it might be interesting– Arthur Siqueira
I resolved with the
.explode
, I kept it in a variable and it worked. Thanks, guys.– Mateus Araujo