Jump to content

BE73

New Members
  • Posts

    4
  • Joined

  • Last visited

BE73's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. Ok, I exactly used your code: while(strpos($insert, "/")!=false) { $insert = preg_replace_callback("/([\d]+)\/([\d]+)/m", function ($m) { list(, $num1, $num2) = $m; if($num1 == 0 || $num2 == 0) $dec = 'division by zero'; else $dec = $num1 / $num2; return $dec; } , $insert); } But I always get this error: <br /><b>Parse error</b>: syntax error, unexpected T_FUNCTION in <b>server/script.php</b> on line <b>58</b><br /> Line 58 is this one: $insert = preg_replace_callback("/([\d]+)\/([\d]+)/m", function ($m) { The same when I try it with: $insert = preg_replace_callback("/([\d]+)\/([\d]+)/m", function divcalc($m) { Do you detect the mistake? Thank you a lot!
  2. Thank you a lot for your excellent advices! @Jacques1: Yes, I would like to output an error message! @Ch0cu3r: I tried to use your version, but I get an error for the third line ($userdiv = preg_re...): <br /><b>Warning</b>: Wrong parameter count for preg_replace_callback() in <b>server/script.php</b> on line <b>59</b><br /> This is the code now: while(strpos($userdiv, "/")!=false) { preg_match_all("/([\d]+)\/([\d]+)/", $userdiv, $sta); $userdiv = preg_replace_callback("/([\d]+)\/([\d]+)/", $userdiv); function division($m) { list(, $num1, $num2) = $m; if($num1 == 0 || $num2 == 0) { $sta = 'division by zero'; } else { $userdiv=str_replace($sta[1][0].'/'.$sta[2][0], (int)$sta[1][0]/(int)$sta[2][0], $userdiv); return $sta; } } } Have I done anything wrong with preg_replace_callback? Thank you very much!
  3. Hello @Jacques1, thank you a lot for your response! That helped me. I changed the variable names. What could I do with the input in order to get no loop? I would like to check everything behind the / if it is zero. For example: 1/20 -->ok 2/0 --> wrong 2/01 --> ok I don't know if I unterstood your last paragraph correct. But the output of floats instead of fractions shouldnt be a problem in this case. Thank you a lot! Kind regards!
  4. Hello altogether, I hope you can help me. I got some fields in a form and one of these is that I want to give the user the opportunity to type in a fraction instead of a decimal digit. Input for example would be: 14/3 or 26/23 etc. Now I got the problem with devision by zero e.g. 12/0 and it seems that I made a mistake with the if query, but I dont find it. Could you please help me? This is the code: while(strpos($insert, "/")!=false) { preg_match_all("/([\d]+)\/([\d]+)/", $insert, $var); if (((int)$var[2][0]) == 0) { $var = "division by zero"; } else { $insert=str_replace($var[1][0].'/'.$var[2][0], (int)$var[1][0]/(int)$var[2][0], $insert); } } ... if ($var != "division by zero") { --> calculating with $insert and output into $var } echo $var; The output of $var should be a decimal digit or "devision by zero". Please note: Everything works fine if there is no devision by zero. (like 16/0 for example) Thank you a lot! Kind regards!
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.