PHP - preg_match - 如何将字符串大写/小写与其之前或之后的任何内容匹配


PHP - preg_match - How to match a string upper/lower case with anything before or after it?

我有一个函数的一部分是这样的:

if (preg_match("#'bscript'b#",$userInput))
{
    $bannedWord = 'script';
    logHax();
    return TRUE;
}

这给我试图完成的事情带来了问题,因为它只会匹配确切的单词"脚本",而不是它的变体,如"ScriPt"或"<script>"。

我想要的是不匹配的字符串以及原始字符串返回 true 的示例。

这是

怎么回事:

if (preg_match("/<script'b[^>]*>/i",$userInput))
{
    $bannedWord = 'script';
    logHax();
    return TRUE;
}

不区分大小写的匹配:

preg_match("#'bscript'b#i",$userInput)

请注意i。 另请注意,这是文档中的第一个示例

<?php
// The "i" after the pattern delimiter indicates a case-insensitive search
if (preg_match("/php/i", "PHP is the web scripting language of choice.")) {
    echo "A match was found.";
} else {
    echo "A match was not found.";
}
?>

干杯

如果你真的想在字符串之前或之后匹配"任何东西"(不仅仅是一个单词),那么你在这里甚至不需要preg_match,bacuse你可以做这样的事情:

$userInputLower = strtolower($userInput);
if (strpos($userInputLower, 'script') !== false)
{
    $bannedWord = 'script';
    logHax();
    return TRUE;
}