简体   繁体   English

正则表达式仅允许字母数字和其他特定字符

[英]Regular Expression only allow alphanumeric plus other specific characters

With a PHP function I'm trying to create a RegEx string for preg_replace that will only allow alphanumeric characters plus ! @ # $ % & . , 使用PHP函数,我试图为preg_replace创建一个RegEx字符串,该字符串仅允许使用字母数字字符加! @ # $ % & . , ! @ # $ % & . ,

The PHP function is PHP函数是

 function clean($var) {
    $regEx="REGEXSTRING";   
    $var = preg_replace($regEx, "", $var);
    return str_replace(array("&", "'"),
    array("&", "'"), $var);
}

What would the string be to match what I'm looking for. 该字符串将是什么,以匹配我要寻找的内容。

:: EDIT :: As I was typing this I figured out what would work for me. ::编辑::当我键入此内容时,我想出了适合自己的方法。 Not sure if it is the best solution but it works. 不知道这是否是最好的解决方案,但它是否有效。 But I figured I'd post it here as a solution for other beginners. 但是我认为我会将其发布在这里,作为其他初学者的解决方案。

The string I used is... 我使用的字符串是...

$regEx="/[^a-zA-Z0-9 !@#$%&.,]/";

Hope it helps someone. 希望它可以帮助某人。

 function clean($var) {
    $regEx="/[^a-zA-Z0-9 !@#$%&.,]/"; 
    $var = preg_replace($regEx, "", $var);
    return str_replace(array("&", "'"),
    array("&", "'"), $var);
}

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

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