允许字母,数字和某些字符删除其余[关闭]

when user submit a form input text

i want it just to allow a-z 0-9 and -_ just this numbers digits and -_

how do i check the input and make it only this inputs allows and delete the rest

Thank You!

$output = preg_replace("/[^a-zA-Z0-9_\-]/", '', $input);
function validate($value){
    return preg_match("/^[a-zA-Z0-9_\-]*$/", $value) !== 0;
}

If the return value is false then you have invalid characters, if it true, everything is correct.

$input = preg_replace("/[^0-9a-z\_\-]/i", "", $_POST["myfield"]);

Use regexp.