$_POST Number and Character checker

折月煮酒 提交于 2019-12-12 05:32:21

问题


This is a repeat question of Check $_POST['id'] is numeric then do this if not do otherwise

But..

The answers given do not answer the question IMHO.

The question is testing a $_POST string to see if it is a number or contains non-numeric characters. Not change the code to use $_GET.

The original question is as such:

if (!empty($_POST['id'])) { 
echo "empty";
} else {
if (is_numeric($_POST['id'])) {
echo "numeric!";
} else {
echo "not empty but not numeric how come?";
}
}

I have tried, is_numeric() , but since $_POST delivers the variable as a string, even if it is a number this is useless.

I have tried, if(!preg_match('#[^0-9]#',$id)), but I have no idea why this doesn't work.

ctype_digit() I believe still has a problem with seeing all things from $_POST as strings, so no good.

There has to be a way, but I'm lost on how to do this. And no I can't use $_GET.

UPDATE ANSWER!!! (TYPO!!!!! god dammit!!!!):

// test.php

// testing with $_POST['id'] from forum with id = 5 and another test where id = new

$id = $_POST['editid'] ;
echo "<br>---".$id."---<br>";

if (empty($id)) { 
echo "<br>1: empty";
} else {
if (!is_numeric($id)) {
echo "<br>2: This is the number 5";
} else {
echo "<br>3: the must be the word new";
}
}

 // test 2 ... ctype_digit


if (empty($id)) { 
echo "<br>4: empty";
} else {
if (!ctype_digit($id)) {
echo "<br>5: This is the number 5";
} else {
echo "<br>6: the must be the word new";
}
}

// test 3 ... 



if (empty($id)) { 
echo "<br>7: empty";
} else {
if (!preg_match('#[^0-9]#',$id)) {
echo "<br>8: This is the number 5";
} else {
echo "<br>9: the must be the word new";
}
}

/**

result from 5


---5---

3: the must be the word new
6: the must be the word new
8: This is the number 5

results from "new"



**/

来源:https://stackoverflow.com/questions/40372642/post-number-and-character-checker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!