PDA

View Full Version : Simple regex question


Lionel
01-04-2006, 12:50 AM
I am getting a $value from a $_POST['value'] from a textbox

How can I make sure that only "digits" and "spaces" are allowed, and nothing else?

eg 55 32 etc...

but no 55-32 or abcd or [ or * or etc...

harmor19
01-04-2006, 12:20 PM
if(!is_numeric($_POST['value']))
{
die('Only numerical values');
}

I don't know if it'll allow spaces

Code Monkey
01-04-2006, 03:14 PM
That will die on spaces. This should work.


$test_one = '1 23465';

if(preg_match('#[^0-9\s]#',$test_one))
{
$result = 'Ahah! Found an evil character.';
}
else
{
$result = 'Only Digits and Spaces here!';
}

echo $result;


You you need to check multiple items use it as a function.

$test_one = '1 23465';

if(digitsOnly($test_one))
{
$result = 'Ahah! Found an evil character.';
}
else
{
$result = 'Only Digits and Spaces here!';
}

echo $result;

function digitsOnly($target)
{
return preg_match('#[^0-9\s]#', $target);
}

Lionel
01-04-2006, 03:42 PM
Both methods stop valid and invalid characters.

Code Monkey
01-04-2006, 05:00 PM
Not according to your original post. Is there something else you were trying to block. The regex above should return false for digits and spaces only, true for anything else.

filburt1
01-04-2006, 07:33 PM
if (preg_match("/^[0-9\w]+$/siU", $subject))
{
// valid
}

or

function validate($subject)
{
for ($i = 0; $i < strlen($subject); $i++)
{
if (!is_numeric($subject{$i}) or $subject{$i} != " ")
{
return false;
}
}
return true;
}

Code Monkey
01-04-2006, 09:04 PM
if (preg_match("/^[0-9\w]+$/siU", $subject))
{
// valid
}

or

function validate($subject)
{
for ($i = 0; $i < strlen($subject); $i++)
{
if (!is_numeric($subject{$i}) or $subject{$i} != " ")
{
return false;
}
}
return true;
}


Not working for me.

The c wrapper ctype_digit() is much faster than is_numeric if your going to go that way about it.


^0-9\s //Anything that does not "^" have the value of a digit "0-9" or a space "\s".
^\d\s works also and might be a bit faster


Run this test on your server Lionel. It works for every combo I have tried.


$test = array(
'55 32',
'123b4',
'4-55',
'9543',
'*/12-b',
'&f54gt',
'[1234]',
'12 3',
'end'
);

for($i = 0; $i < 9; $i++)
{
echo digitsOnly($test[$i]) . '<br />';
}



function digitsOnly($target)
{
if(preg_match('#[^\d\s]#', $target))
{
return 'Ahah! Found an evil character in <b>' . $target . '</b>';
}
else
{
return 'Only Digits and Spaces here! in <b>' . $target . '</b>';
}
}

Lionel
01-06-2006, 11:58 PM
I finally got it to work by exploding the values in the array and extracting them individually.


if (!empty($_POST['predminutes1'])){
$count1 = 0;
foreach($_POST['predminutes1'] as $mkey1=>$gvalue1) {
$gsub1 = explode(' ', $gvalue1);
foreach($gsub1 as $gnumber1) {
# check value to add to count
if(!empty($gnumber1))
$count1++;
if ($count1 > $predscore1) {
eval(print_standard_error('error_invalidscorers')) ;
}
if (!preg_match('/^([0-9]*)$/', $gnumber1)){
eval(print_standard_error('error_invalidcharacters '));
}
}
}
}

akanevsky
01-07-2006, 01:25 AM
if (preg_match("#^[\d\s]*$#", $subject))
{
// valid
}

Code Monkey
01-07-2006, 01:31 AM
I finally got it to work by exploding the values in the array and extracting them individually.


if (!empty($_POST['predminutes1'])){
$count1 = 0;
foreach($_POST['predminutes1'] as $mkey1=>$gvalue1) {
$gsub1 = explode(' ', $gvalue1);
foreach($gsub1 as $gnumber1) {
# check value to add to count
if(!empty($gnumber1))
$count1++;
if ($count1 > $predscore1) {
eval(print_standard_error('error_invalidscorers')) ;
}
if (!preg_match('/^([0-9]*)$/', $gnumber1)){
eval(print_standard_error('error_invalidcharacters '));
}
}
}
}

That's a lot of code to do a simple task.

Lionel
01-07-2006, 01:57 AM
not really I just incorporated one line of code into an existing loop.