简体   繁体   English

如何在php中删除要插入mysql数据库的特殊字符

[英]How to remove special characters to be get inserted into mysql database in php

I have a textfield and if any special characters are attached to the string the value should not be inserted into the database.我有一个文本字段,如果字符串附加了任何特殊字符,则不应将值插入到数据库中。 How can this be achieved in php?这如何在 php 中实现? Thanks for the help in advance.我在这里先向您的帮助表示感谢。

 if (preg_match('/[\'^£$%&*()"}{@#~?><>,|=_+¬-]/', $i)) {
  echo '<script language="javascript">'; 
  echo 'alert("Enter a valid Insurance Type.")'; echo '</script>'; 
}

If by "special" you mean non alpha-numeric, you could do something like this to strip out those characters before inserting them:如果“特殊”是指非字母数字,则可以执行以下操作以在插入之前删除这些字符:

preg_replace("/[^a-zA-Z0-9\s\p{P}]/", "", $your_string);

Otherwise you could just check if the string is alpha-numeric, and only insert the record if it is:否则,您可以只检查字符串是否为字母数字,如果是,则仅插入记录:

if (ctype_alnum($your_string)) {
    // Do your MySQL insert
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM