PHP:检查无效字符的最快方法(除了 az、AZ、0-9、#、-、.、$)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1735972/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:40:04  来源:igfitidea点击:

PHP: fastest way to check for invalid characters (all but a-z, A-Z, 0-9, #, -, ., $)?

phpregexvalidation

提问by Tom

I have to check the buffer input to a PHP socket server as fast as possible. To do so, I need to know if the input message $buffer contains any other character(s) than the following: a-z, A-Z, 0-9, #, -, . and $

我必须尽快检查 PHP 套接字服务器的缓冲区输入。为此,我需要知道输入消息 $buffer 是否包含除以下字符以外的任何其他字符:az、AZ、0-9、#、-、. 和 $

I'm currently using the following ereg function, but wonder if there are ways to optimize the speed. Should I maybe use a different function, or a different regex?

我目前正在使用以下 ereg 函数,但想知道是否有优化速度的方法。我应该使用不同的函数还是不同的正则表达式?

if (ereg("[A-Za-z0-9]\.\#\-$", $buffer) === false)
{
    echo "buffer only contains valid characters: a-z, A-Z, 0-9, #, -, ., $";
}

回答by Gumbo

Try this function:

试试这个功能:

function isValid($str) {
    return !preg_match('/[^A-Za-z0-9.#\-$]/', $str);
}

[^A-Za-z0-9.#\-$]describes any character that is invalid. If preg_matchfinds a match (an invalid character), it will return 1and 0otherwise. Furthermore !1is falseand !0is true. Thus isValidreturns falseif an invalid character is found and trueotherwise.

[^A-Za-z0-9.#\-$]描述任何无效的字符。如果preg_match找到匹配项(无效字符),则返回1,否则返回0。此外!1假的!0真的。因此,如果发现无效字符,则isValid返回false,否则返回true

回答by Richard

Only allowing characters a-z uppercase or lowercase..

只允许字符az大写或小写..

if (preg_match("/[^A-Za-z]/", $FirstName))
{
    echo "Invalid Characters!";
}

Adding numbers..

添加数字..

if (preg_match("/[^A-Za-z0-9]/", $FirstName))
{
    echo "Invalid Characters!";
}

Adding additional characters to allow (in this case the exclamation mark)..

添加额外的字符以允许(在这种情况下为感叹号)。

(Additional characters must be preceded with an "\" as shown.)

(附加字符必须以“\”开头,如图所示。)

if (preg_match("/[^A-Za-z0-9\!]/", $FirstName))
{
    echo "Invalid Characters!";
}

回答by Richard Sim?es

The pregfamily of functions is quite a bit faster than ereg. To test for invalid characters, try something like:

preg系列函数是相当多的要快ereg。要测试无效字符,请尝试以下操作:

if (preg_match('/[^a-z0-9.#$-]/i', $buffer)) print "Invalid characters found";

回答by Alan Storm

You'll want to shift over to using preginstead of ereg. The eregfamily of functions have been depreciated, and (since php 5.3) using them will throw up a PHP warning, and they'll be removed from teh language soon. Also, it's been anecdotal wisdom that the preg functions are, in general, faster than ereg.

你会想要转移到 usingpreg而不是ereg。该ereg系列函数已被弃用,并且(自 php 5.3 起)使用它们将引发 PHP 警告,并且它们将很快从语言中删除。此外,据传闻,preg 函数通常比 ereg 快。

As for speed, based on my experience and the codebases I've seen in my career, optimizing this kind of string performance would be premature at this point. Wrap the comparision in some logical function or method

至于速度,根据我的经验和我在职业生涯中看到的代码库,此时优化这种字符串性能还为时过早。将比较包装在一些逻辑函数或方法中

//pseudo code based on OP 
function isValidForMyNeeds($buffer)
{
    if (ereg("[A-Za-z0-9]\.\#\-$", $buffer) === false)
    {
        echo "buffer only contains valid characters: a-z, A-Z, 0-9, #, -, ., $";
    }
}

and then when/if you determine this is a performance problem you can apply any needed optimization in one place.

然后当/如果您确定这是一个性能问题,您可以在一个地方应用任何需要的优化。

回答by Douwe Maan

preg_match is both faster and more powerful than ereg:

preg_match 比 ereg 更快、更强大:

if(preg_match('/^[^a-z0-9\.#\-$]*$/i', $sString) > 0) //check if (doesn't contain illegal characters) is true
{
  //everything's fine: $sString does NOT contain any illegal characters
}

or turn it around:

或转过来:

if(preg_match('/[a-z0-9\.#\-$]/i', $sString) === 0) //check if (contains illegal character) is false
{
  //everything's fine: $sString does NOT contain any illegal characters
}

回答by Galen

Use preg isntead, its faster, and ereg has been discontinued.

使用 preg istead,它更快,并且 ereg 已停产。