如何在php中使用数组检查字符串是否包含某些字符

如何在php中使用数组检查字符串是否包含某些字符,php,arrays,string,Php,Arrays,String,我需要检查字符串是否包含非法字符,我想知道是否有一种方法可以通过数组执行,因为我有一个允许的数组字符。这是用PHP编写的 阵列,以防您想看到它: $MsgAry1 = array("A","B","C","D","E","F","G","H","I","J", "K","L","M","N","O","P","Q","R","S","T", "U","V","W","X","Y","Z","a","b","c","d", "e",

我需要检查字符串是否包含非法字符,我想知道是否有一种方法可以通过数组执行,因为我有一个允许的数组字符。这是用PHP编写的

阵列,以防您想看到它:

$MsgAry1 = array("A","B","C","D","E","F","G","H","I","J",
           "K","L","M","N","O","P","Q","R","S","T",
           "U","V","W","X","Y","Z","a","b","c","d",
           "e","f","g","h","i","j","k","l","m","n",
           "o","p","q","r","s","t","u","v","w","x",
           "y","z","1","2","3","4","5","6","7","8",
           "9","0",".",",","'","?","!"," ","_","-");

谢谢您的帮助。

您可以使用类似的方法,str\u split将字符串拆分为字符,然后您可以使用in\u数组检查每个字符:

可以使用str_split和in_array函数执行此操作,如下所示:

$MsgAry1 = array("A","B","C"); //Add your actual array here
$testCase = "Hello, World!";
$testCase_arr = str_split($testCase);

foreach($testCase as $test)
{
    if(!in_array($test, $MsgAry1))
    {
        echo "Error! Invalid Character!";
        break(); //Or exit();, as needed
    }
}

您不需要循环…但是如果您确实使用了循环,那么您最好使用提前返回/中断,这样您就不会在发现不合格字符后进行不必要的迭代

在我自己的一个项目中,我更喜欢存储正则表达式而不是一个字符数组——它更简洁、更直接

如果必须创建一个字符数组,则可以通过修剪所有安全字符并检查修剪后是否保留任何字符来避免分解和循环

代码:

输出:

test match: true
test trim:  true
---
1234 match: true
1234 trim:  true
---
w0ws3r! match: true
w0ws3r! trim:  true
---
##tag## match: false
##tag## trim:  false
---
p、 如果你使用trim的字符掩码路径,那么从技术上来说你可以稍微缩小你的数组,因为字符掩码最多只能接受一个字符范围

$MsgAry1 = ["A-Z","a","b","c","d",
           "e","f","g","h","i","j","k","l","m","n",
           "o","p","q","r","s","t","u","v","w","x",
           "y","z","1","2","3","4","5","6","7","8",
           "9","0",".",",","'","?","!"," ","_","-"];

这里是一个非迭代方法,它的执行速度应该比循环中的现有答案更快

function containsIllegalChars($testString) {
    static $MsgAry1 = array("A","B","C","D","E","F","G","H","I","J",
           "K","L","M","N","O","P","Q","R","S","T",
           "U","V","W","X","Y","Z","a","b","c","d",
           "e","f","g","h","i","j","k","l","m","n",
           "o","p","q","r","s","t","u","v","w","x",
           "y","z","1","2","3","4","5","6","7","8",
           "9","0",".",",","'","?","!"," ","_","-");
    $foundCount = 0;
    str_replace($MsgAry1, '', $testString, $foundCount);
    return $foundCount > 0;
}
通过删除所有大写字母并使用str_ireplace,您可以更有效地处理特定的字母集

function containsIllegalChars($testString) {
    static $MsgAry1 = array("a","b","c","d",
           "e","f","g","h","i","j","k","l","m","n",
           "o","p","q","r","s","t","u","v","w","x",
           "y","z","1","2","3","4","5","6","7","8",
           "9","0",".",",","'","?","!"," ","_","-");
    $foundCount = 0;
    str_ireplace($MsgAry1, '', $testString, $foundCount);
    return $foundCount > 0;
}

这将显示无效字符,如磅符号?是的。它将为您提供的有效字符数组中未包含的任何字符显示错误消息,在本例中为$MsgAry1。干杯!很高兴我能帮忙
$MsgAry1 = ["A-Z","a","b","c","d",
           "e","f","g","h","i","j","k","l","m","n",
           "o","p","q","r","s","t","u","v","w","x",
           "y","z","1","2","3","4","5","6","7","8",
           "9","0",".",",","'","?","!"," ","_","-"];
function containsIllegalChars($testString) {
    static $MsgAry1 = array("A","B","C","D","E","F","G","H","I","J",
           "K","L","M","N","O","P","Q","R","S","T",
           "U","V","W","X","Y","Z","a","b","c","d",
           "e","f","g","h","i","j","k","l","m","n",
           "o","p","q","r","s","t","u","v","w","x",
           "y","z","1","2","3","4","5","6","7","8",
           "9","0",".",",","'","?","!"," ","_","-");
    $foundCount = 0;
    str_replace($MsgAry1, '', $testString, $foundCount);
    return $foundCount > 0;
}
function containsIllegalChars($testString) {
    static $MsgAry1 = array("a","b","c","d",
           "e","f","g","h","i","j","k","l","m","n",
           "o","p","q","r","s","t","u","v","w","x",
           "y","z","1","2","3","4","5","6","7","8",
           "9","0",".",",","'","?","!"," ","_","-");
    $foundCount = 0;
    str_ireplace($MsgAry1, '', $testString, $foundCount);
    return $foundCount > 0;
}