我不能让pregmatch接受单引号(')或双引号(“)。我在php中试图将这个模式放在”textarea“中。
我想允许两种报价类型,但都不被接受。因此暂停输入到数据库中。
我的模式是
pattern="[a-zA-Z0-9.,()'!?:"\s]"
我遇到的preg比赛是
!preg_match("/^[a-zA-Z0-9.,()'!?:"\s]*$/", $description)
我搜索并尝试了一切。
当我试试这个
"/^['\a-zA-Z0-9.,()!?:\s]*$/"
它让丽莎回归而不是丽莎
Filter the input,然后运行你的支票,以确保你没有存储malicious bit of HTML or Javascript, or a tag breaker。然后,一旦你到达binding your parameters with PDO插入表明它是一个PARAM_STR。
$description = filter_input(INPUT_POST,'description',FILTER_SANITIZE_STRING);
if(preg_match("~^[a-zA-Z0-9.,()\"'!?:\s]*$~", $description)){
//Call functions to prepare statements and insert into db here.
//...
$stmt->bindValue(':description', $description, PDO::PARAM_STR);
}
由于你的preg匹配现在允许单引号让它离开它并在插入数据库之前删除斜线。
试试这个:
!preg_match("/^['\a-zA-Z0-9.,()!?:\s]*$/", $description))
然后在准备好的语句中绑定参数之前,添加以下代码行:
$description = stripslashes(htmlentities($description));
@John Conde是对的。确保始终使用预准备语句并绑定参数。
希望这是有帮助的