Given URL like below
www.example.com?1+1
How am I supposed to parse the data and do the math?
If it was something like
www.example.com?a=1&op=+&b=1
Then I can just do $_GET[“variable_name”].
But what can I do with the former? What is the best way to parse such query?
I searched for a solution but cannot seem to find something relevant with my current knowledge. I tried looking at $_SERVER but I don't think this helps.
If you are sure that nothing malicious would be passed (or you are just testing), then you could do something like:
$output = eval('return ' . parse_url($_SERVER['REQUEST_URI'])['query'] . ';');
echo $output;
Obligatory notice:
Caution The eval() language construct is very dangerous because it allows execution of arbitrary PHP code. Its use thus is discouraged. If you have carefully verified that there is no other option than to use this construct, pay special attention not to pass any user provided data into it without properly validating it beforehand.
However , I would advise you use the following method and parse the expression with parser in the linked question:
$exp = parse_url($_SERVER['REQUEST_URI'])['query']; // 1+1
Reading Material
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.