简体   繁体   English

借助Regex进行字符串验证-PHP

[英]String Validation with help of Regex - PHP

I want a regex that can validate RegX to ensure no white space/escape characters can be used, especially CR and LF . 我想要一个可以验证RegX的正则表达式,以确保不能使用空格/转义字符,尤其是CRLF

Any help would be appreciated. 任何帮助,将不胜感激。

如果您使用类似下面的内容,它将从字符串中去除空格。

$stripped_string = preg_replace("/[\s]/", "", $string);

Regex is not needed here. 这里不需要正则表达式。 Just use method strpos() , here's reference . 只需使用strpos()方法, 这里是参考

And use it to see, if there are any invalid characters rpesent: 并使用它来查看是否存在任何无效字符rpesent:

$needles = array("\n", "\r", "\t", " ", ...);
foreach($needles as $needle) {
    $res = strpos($haystack, $needle, $offset);
    if ($res !== false) ...do something
}

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

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