今天我编写一些脚本,发现有点小麻烦,使用定义的模式.
模式是创建的文件序列中的开始位置 0 (零) 和不能重复,直到 Z.
示例:
myfile0.ext, myfile1.ext, myfile2.ext, (...), myfile9.ext, myfileA.ext, myfileB.txt, (...), myfileZ.txt
很好, 这不是一个大麻烦,所以我没有使用此代码.
// Filename $seq = $last_used_seq = ''; $seqs = array_merge(range('0','9'), range('A', 'Z'));] $l = 1; while (!in_array($seq, $seqs)) { $seq = chr(ord($last_used_seq) + $l++); }
但
$seq
做不了的预期值 0 (零) 第一次运行. 相反, 它是空白.
调试的变量, 看到了, while 从来没有计算结果为 true. 试图复制在命令行上发现
in_array($seq, $seqs);
始终返回 true. 我试着用 “”, “R” 无论什么价值,我用, 仍在返回 true.
所以我更改为使用 STRICT 理由 in_array to true and works for ‘;A’; through ‘;Z’;, but not for ‘;0’; through ‘;9’;.
while (!in_array($seq, $seqs, true)) { $seq = chr(ord($last_used_seq) + $l++); }
Damn…; PHP 是正确, “;0”; 严格来说并不等于 0. 的 Chr 函数返回值 string 和
范围('' 0, '' 9)
创建 array 与 integer 值.
所以, 我改变了方法来评估所有的值 STRICT, 因为我想要创建一个干净整洁的代码没有其他要使用的函数.
This is the final code that I’;m 使用:
// 最初的值 $seq = ';
$seqs = array_merge(范围(ord('' 0),ord('' 9)), 范围(ord(' A'), ord(' Z')));
$seqs = array_map(chr', $seqs);
$l = 1;
同时 (!in_array($seq, $seqs, 真正的)) {
$seq = chr(ord($新闻电台['' last_seq]) + $l + +);
}
// 文件名 foreach ($itens_for_files as $key => $条 】) {
// ... 另一种代码,以填充文件 $seq = chr(ord($seq) + 1);
同时 (!in_array($seq, $seqs, 真正的)) {
$seq = chr(ord($seq) + 1);
$文件名 = myfile . $seq . ''.ext;
// ...
}
你甚至可以看到, 我改变了 $seqs initial values from ‘;0’; 到你 ASCII 代码和回到你给了我的价值 array 与中的所有值 string 类型.
见你!