-2

So I have been trying to bring back to life my very old website.

I started with replacing ereg with preg, but it's been a very long time since I have written any PHP.

At the moment I am stuck on this:

$_POST['amount'] = preg_replace("/[^0-9/]",'',$_POST['amount']);
$_POST['amount'] = round($_POST['amount']);

if (!preg_match('/[^0-9]/', $_POST['amount'])) {
    echo "Invalid amount.";
}else {
     echo "Passed";
}

I'm not entirely sure where I am going wrong. Should it be !preg_match or preg_match for example?

Edit: $_POST['amount'] allows a user to enter a number and needs to replace anything else other than a number if attempted.

SDZ
  • 726
  • 2
  • 8
  • 21
  • @RiggsFolly I saw the post you said is the same as mine but it didn't seem to make sense to me, it seems different to what I am trying to achieve...? – SDZ Jul 12 '20 at 15:37
  • 1
    [`is_numeric()`](https://www.php.net/manual/en/function.is-numeric.php) seems like the simplest. Or maybe [`filter_var()`](https://www.php.net/manual/en/function.filter-var.php) – RiggsFolly Jul 12 '20 at 15:44
  • I'm not sure what Floating point numbers are but users will be required to enter a number like 1234. What I am trying to use preg_replace and preg_match for is so the script stop/replaces entries like -1,2)3$4 to 1234. Hope that makes sense... – SDZ Jul 12 '20 at 15:45
  • Always dangerous assuming you can second guess what the user ment to put in. Best to say, this aint right, have another try – RiggsFolly Jul 12 '20 at 15:46
  • Well I get what you mean but I have a JavaScript on the entry field which coverts the entry into currency. So if I enter 1234, it will make it £1,234 and this is really why I have this check on the PHP side. – SDZ Jul 12 '20 at 15:48
  • That seems to be a longer way than just stripping down what they enter into just number... – SDZ Jul 12 '20 at 15:54
  • Just numbers go into the DB, once this is fixed anyway... – SDZ Jul 12 '20 at 15:55
  • 1
    In all this time you could have had a simple jiggle with that preg_replace and come up with `preg_replace("/[^0-9]/",'',$_POST['amount']);` Or looked at the answer below which I notice is basically saying the same thing – RiggsFolly Jul 12 '20 at 16:01
  • 1
    And if they are entering money with pence like `£999.00` ___Floating Point Basically___ then an addition of a dot would cope with that. `preg_replace("/[^0-9.]/",'', $_POST['amount']);` – RiggsFolly Jul 12 '20 at 16:04
  • That's what I have but it's not working. Can you see the error in my code? – SDZ Jul 12 '20 at 17:03
  • You mean you changed your code to one I suggested above? – RiggsFolly Jul 12 '20 at 17:14
  • Or you think that your original code and my suggested or @jasonklai suggestions are the same? – RiggsFolly Jul 12 '20 at 17:26

1 Answers1

1

Not sure what the error is (i.e. what is the current output and what was expected). However, one mistake in the prompt is that the pattern for the first preg_replace appears to have a typo: /[^0-9/] should probably be /[^0-9]/

preg_replace("/[^0-9]/",'',$_POST['amount']);

The rest looks like correct syntax (i.e. preg_match returns 0 if there is no match).

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Jason K Lai
  • 1,500
  • 5
  • 15