How "preg_match" only numbers?

2

My failed attempt:

$post_id = preg_match("/^[0-9]+$/", $_POST['post_id']);
  

I tried to be a user who changed the post id for example: post_id="43223646", but when the data is entered in mysql, they are transformed into the number 1, how to make it work correctly?

    
asked by anonymous 27.04.2015 / 13:44

1 answer

4

The preg_match of PHP returns 1 if the expression matched, 0 if it did not match and false if an error occurred.

In your case, you are returning 1 because you are combining.

But since you want the value itself, you should use the third parameter:

  

matches
  If matches is provided, it will be populated with the search results.

Do this:

preg_match("/^[0-9]+$/", $_POST['post_id'], $matches);
$post_id = $matches[0];

Documentation

    
27.04.2015 / 14:01