Sunday 24 November 2019

php: how to sanitize many variables at the same time?



This are my php functions:



function test($a,$b,$c) {
sanitize($a,$b,$c);
echo "$a $b $c";
}


function test2($m,$n) {
sanitize($m,$n);
echo "$m $n";
}

function sanitize() {
// escape strings with stripslashes (and other filters later)
}

test2("he'llo", "wo'rld");

test("he'llo", "wo'rld","hap'y");


Is it possible to keep test and test2 function ?



I just want to avoid having 3 lines :



$a=sanitize($a); 
$b=sanitize($b);
$c=sanitize($c);



and have just:



sanitize($a, $b, $c);

Answer



Php 5.6+



function sanitize( &...$args){

foreach($args as &$arg){
//your sanitising code here, eg:
$arg = strtolower($arg);
}
}

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...