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

时间:2015-08-21 10:29:44

标签: 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","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",".",",","'","?","!"," ","_","-");

感谢您的帮助。

5 个答案:

答案 0 :(得分:4)

您可以使用str_splitin_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
    }
}

答案 1 :(得分:2)

您可以使用类似的内容,str_split将字符串拆分为字符,然后您可以使用in_array检查每个字符:

$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",".",",","'","?","!"," ","_","-");

$string = "Hello";

foreach (str_split($string) as $char) {
        if (in_array($char, $MsgAry1)) {
            echo "Char: ".$char." -> OK";
        } else {
            echo "Char: ".$char." -> KO";
        }   
}

答案 2 :(得分:1)

$resultarray = preg_replace("/[^A-Za-z0-9]/", "", $MsgAry1);
print_r ($resultarray);

答案 3 :(得分:1)

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

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;
}

答案 4 :(得分:0)

您不需要循环...但是,如果您确实使用了循环,则最好使用早期的return / break,这样以后就不必进行不必要的迭代了您发现了失格的角色。

在我自己的项目中,我希望存储正则表达式而不是字符数组-更加简洁直接。

如果必须包含一个字符数组,则可以通过修剪所有安全字符并检查修剪后是否还有任何字符来避免爆炸和循环。

代码:(Demo

$MsgAry1 = ["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",".",",","'","?","!"," ","_","-"];

$strings = [
    'test',
    '1234',
    'w0ws3r!',
    '##tag##',
];

foreach ($strings as $string) {
    echo "$string match: " . var_export(!preg_match('~[^\w.,\'?! -]~', $string), true) . "\n";
    echo "$string trim:  " . var_export(!strlen(ltrim($string, implode($MsgAry1))), true) . "\n---\n";
}

输出:

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.s。如果您使用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",".",",","'","?","!"," ","_","-"];