0

With code something like this

$tk = intval($_GET['tk']);

$vosa = $_GET['vosa'];

echo $tk*100*$vosa;

Where $vosa is a string of something like 0.0425/1920*60*8. I'd need it replaced, without being calculated first, into the echo and then echo the entire thing $tk*100*0.0425/1920*60*8 result. How could I achieve this?

2
  • Is it really necessary to transmit the string with GET? Commented Jun 21, 2017 at 8:52
  • eval.in/819834 Check This Commented Jun 21, 2017 at 8:59

5 Answers 5

1

Ok another version. Replace the values in your string with sprintf.

echo sprintf("%s*100*%s", (string)$tk, (string)$vosa);

if %d for digit don't match your case then you can use %s. You use in your case directly $_GET variables. So sprintf is a good choice. I have tested it with:

php -r 'echo sprintf("%s*100*%s", "123", "4.000");'

output:

123*100*4.000
Sign up to request clarification or add additional context in comments.

3 Comments

It will then need to be calculated aswell
That will actually evaluate both $tk and $vosa as integers, and if $vosa is, say, "200*15", it will treat it as if it was just 200.
Thats why i wrote use %s for string. And convert your value to string.
1

To output, just echo the string:

echo "{$vosa} = {$result}";

Your problem is how to calculate $result from $vosa.

A very risky way would be to use eval() - or as someone sometimes calls it, evil().

The risk is that I could send you a vosa value of system('FORMAT C: /AUTOTEST') (which would not work, but you get my meaning).

// vosa='/bin/dd if=/dev/zero of=etc etc'
// This will return zero. It will return a whole lot of zeroes
// all over your hard disk.
$result = eval("return {$tk}*100*{$vosa};");

Possibly, validating $vosa with a regular expression could help, at least as long as you use simple expressions.

Alternately, you must implement an expression parser.

This is another ready made. You would use it like this:

include('./some/where/mathparser.php');

$parser = new MathParser();
$parser->setExpression("{$tk}*100*{$vosa}");
$result = $parser->getValue();

echo "The result of {$tk}*100*{$vosa} is {$result}.";

Comments

0

You can use string and then use eval to execute it as a php code:

<?php

$tk = intval($_GET['tk']);
$vosa = $_GET['vosa'];
echo eval("return $tk*100*$vosa;");

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.

Comments

0
$tk = intval($_GET['tk']);    
$vosa = $_GET['vosa'];  // "0.0425/1920*60*8"
$ans = eval('return '.$vosa.';');
echo $ans;
echo "<br>";
echo $tk*100*$ans;

Example : https://eval.in/819834

3 Comments

This seems to vcalculate $vosa beforehand? That will mess up the correct result for me. I might just be better of with a string split and some intval...
Sorry ,.,, Can You Explain me What you want ?
Wait, actually no.... if you see here: pastebin.com/q9i5Sm6v the answer is 29.7... meanwhile I know the correct one is 33.56. To mee, it looks like vosa is precalculated and then replaced... i'd need it to just be replaced, and then everything calculated. vosa must not be calculated AT ALL before being placed into the final calculation
0

Got it myself

<?php

$tk = $_GET['tk'];
$aeg = $_GET['aeg'];
$kfc = $_GET['kfc'];
$vosa = $_GET['vosa'];

$final = $tk.'*'.$aeg.'*'.$kfc.'*'.$vosa;

$ans = eval('return '.$final.';');
echo round($ans,2);

1 Comment

That's okay. But remember that a value of tk equal to system('dd if=/dev/zero of=/your/harddisk');//) is likely to have undesirable results.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.