我的功能包括:
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);
}
$output
只是我测试它的方式。每当我在表单中添加一个值,比如说aa!!\||\
,然后调用该函数,如果我将in_array检查设置为true,我总是得到一个0或1的完整数组
帮助将得到很好的应用
威尔
答案 0 :(得分:0)
我真的不明白为什么!
似乎在数组中没有。无论如何,我已经查看了in_array
文档。诀窍是将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);
输出是:
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
答案 1 :(得分:-1)
尝试使用$ 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);
}