1
How do I take only what is after ? and before &?
$url = "https://www.dominio.com/login?erro=1&data=2018-03-06";
$url_cod = explode('?', $url);
There he is returning everything that comes after the question mark.
1
How do I take only what is after ? and before &?
$url = "https://www.dominio.com/login?erro=1&data=2018-03-06";
$url_cod = explode('?', $url);
There he is returning everything that comes after the question mark.
2
If you want to take the parameters passed by GET from any URL - not necessarily the current page, which PHP already makes available in $_GET
- there are specific functions for this:
parse_url
receives a URL and returns array with the main components - including querystring, which is what matters here.
parse_str
is made to break querystrings in array.
Bringing the two together:
$url = "https://www.dominio.com/login?erro=1&data=2018-03-06";
$partes = parse_url($url);
if(!empty($partes['query'])) {
$vars = [];
parse_str($partes['query'], $vars);
var_dump($vars);
}
// Saída:
//
// array(2) {
// ["erro"]=>
// string(1) "1"
// ["data"]=>
// string(10) "2018-03-06"
// }
About the approach you were trying:
How do I catch only what is after ? and before &?
Thinking about this problem in a generic way (because to treat Urls I would use the code above), it is simple to solve with string manipulation. With the explode
you already managed to get a string like this:
$sua_string = "erro=1&data=2018-03-06";
Looking at that, just find the first &
and take everything before him:
$sua_string = "erro=1&data=2018-03-06";
$pos = strpos($sua_string, '&');
$resultado = substr($sua_string, 0, $pos-1);
echo $resultado; // erro=1
1
Anything that comes after ?
is populated by the super global $_GET
, so, if you want to take some parameter in the url you can do as follows:
$data = $_GET['data'];
This code would return the value '2018-03-06'
.
To catch the mistake just do:
$erro = $_GET['erro'];
This code would return the value 1
.
Since you want the value with the variable name, you can concatenate the value with a string.
$erroString = 'erro='.$erro;
I need it to return only the value: error=1
I edited it. You can get any value from the url, just pass the corresponding key
Not only do I need the value, I need it to return with the word error too.
Only you concatenate the value obtained with the string you want. I edited the answer doing this.
0
The explode will turn $url_cod into an array, to pick up what is before just put:
$parte_url = $url_cod[1];
//Ou [0] para oque vem antes
For the '&' you use the same system
Or if you want to get the GET parameters, do as @Phelipe answered above
$url = "www.google.com?&nome_da_variavel_recebida=123";
$valor = $_GET['nome_da_variavel_recebida'];
//Nesse caso $valor seria igual a 123
Browser other questions tagged php
You are not signed in. Login or sign up in order to post.
To the URL,
?erro=1&data=2018-03-06
or?data=2018-03-06&erro=1
are the same thing, so what value should you take? And why do you need to do this?– Woss
I need to get only error=1, is that I am doing a validation via GET on a page.
– Wendler
But it is the URL of the page itself or any URL as in the example?
– bfavaretto
From the page itself. I said wrong, via GET will not work, because I need it to return with the word error, exactly like this: error=1
– Wendler