简体   繁体   English

PHP Foreach in_array()无法正常工作

[英]PHP Foreach in_array() not working

I have a function comprising of: 我的功能包括:

function check_name($name)
{
    // Possibly slower than using regular expressions but since they're giving me grief, this function will scan a string for characters that aren't found in a human name 
    $invalid_characters = 0;
    $disallowed_chars = array_merge(range(0, 9), range("a", "z"), range("A", "Z"), array(".", "'", "-"));
    // This array contains the characters I want to allow. If the character is not in the array, the counter is updated 
    $name = str_split($name);
    foreach($name as $letter)
    {
        if(in_array($letter, $disallowed_chars)==FALSE)
        {
            $invalid_characters = $invalid_characters + 1;
            $output[] = 1;
        }   
        else
        {
            $output[] = 0;
        }
    } 
    return array($invalid_characters, implode("", $name), $output);
}

The $output is just my way of testing it. $output只是我测试它的方式。 Whenever I put a value into the form, say aa!!\\||\\ , and call the function, I always get an entire array of 0s or 1s if I set the in_array check to true 每当我将一个值放入表单中时,例如aa!!\\||\\ ,然后调用该函数,如果将in_array检查设置为true,我总会得到整个0或1s的数组。

Help would be greatfully apprchiated 帮助将不胜感激

Will

I do not really understand it why ! 我真的不明白为什么! seems it's in the array when not. 似乎它不在数组中。 Anyway, I've checked the in_array documentation. 无论如何,我已经检查了in_array文档。 The trick is to set the in_array to strict mode (add a third param as true). 诀窍是将in_array设置为严格模式(将第三个参数添加为true)。

function check_name($name) {
    // Possibly slower than using regular expressions but since they're giving me grief, this function will scan a string for characters that aren't found in a human name 
    $invalid_characters = 0;
    $disallowed_chars = array_merge(range(0, 9), range("a", "z"), range("A", "Z"), array(".", "'", "-"));
    // This array contains the characters I want to allow. If the character is not in the array, the counter is updated 
    $letters = str_split($name);
    foreach ($letters as $letter) {
        if (in_array($letter, $disallowed_chars, true)) {
            $invalid_characters++;
            $output[] = 1;
        } else {
            $output[] = 0;
        }
    }
    return array($invalid_characters, $name, $output);
}
$string = "aa!!\\||\\";

$o = check_name($string);

Output is: 输出为:

array (size=3)
  0 => int 2
  1 => string 'aa!!\||\' (length=8)
  2 => 
    array (size=8)
      0 => int 1
      1 => int 1
      2 => int 0
      3 => int 0
      4 => int 0
      5 => int 0
      6 => int 0
      7 => int 0

Try This use $output_array instead $output,, 尝试使用$ output_array代替$ output,

function check_name($name)
{
    // Possibly slower than using regular expressions but since they're giving me grief, this function will scan a string for characters that aren't found in a human name 
    $invalid_characters = 0;
    $disallowed_chars = array_merge(range(0, 9), range("a", "z"), range("A", "Z"), array(".", "'", "-"));
    // This array contains the characters I want to allow. If the character is not in the array, the counter is updated 
    $name = str_split($name);
    foreach($name as $letter)
    {
        if(in_array($letter, $disallowed_chars)==FALSE)
        {
            $invalid_characters = $invalid_characters + 1;
            $output = 1;

        }   
        else
        {
            $output = 0;
        }
            $output_array = array_push($output);
    } 
    return array($invalid_characters, implode("", $name), $output);
}

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

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