English 中文(简体)
PHP - Sanitise a comma separated string
原标题:

What would be the most efficient way to clean a user input that is a comma separated string made entirely on numbers - e.g

2,40,23,11,55

I use this function on a lot of my inputs

function clean($input){ $input=mysql_real_escape_string(htmlentities($input,ENT_QUOTES)); return $input; }

And on simple integers I do:

if (!filter_var($_POST[ var ], FILTER_VALIDATE_INT)) {echo( error - bla bla ); exit;}

So should I explode it and then check every element of the array with the code above or maybe replace all occurrences of , with and then check the whole thing is a number? What do you guys think?

最佳回答
if (ctype_digit(str_replace(",", "", $input))) {
  //all ok. very strict. input can only contain numbers and commas. not even spaces
} else {
  //not ok
}

If it is CSV and if there might be spaces around the digits or commas and maybe even some quotation marks better use a regex to check if it matches

问题回答
if (!preg_match( /Ad+(,d+)*z/ , $input)) die( bad input );

If you want to transform a comma-separated list instead of simply rejecting it if it s not formed correctly, you could do it with array_map() and avoid writing an explicit loop.

$sanitized_input = implode(",", array_map("intval", explode(",", $input)));

I would filter instead of error checking on simple input, though only cause I m lazy, I suppose, and usually in a web context there s way too many cases to handle on what could be coming in that I wouldn t expect: Simple filter below.

<?php
$input =  234kljsalkdfj234a,a, asldkfja 345345sd,f jasld,f234l2342323@#$@# ;
function clean($dirty){ // Essentially allows numbers and commas, just strips everything else.
    return preg_replace( /[^0-9,]/ , "", (string) $dirty);
}

$clean = clean($input);

echo $clean;
// Result: 234234,,345345,,2342342323
// Note how it doesn t deal with adjacent filtered-to-empty commas, though you could handle those in the explode.  *shrugs*

?>

Here s the code and the output on codepad:

http://codepad.org/YfSenm9k





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签