带有前导零的PHP输出编号不正确


PHP output number with leading zeros incorrectly

我在这样的文件中有一行:

0000000/BirthstoneEnsemble/f/0/1380152724

我被$pieces = explode("/", $line); 引爆

当我执行echo $pieces[0] == "0000000"时,它返回false。我尝试将pieces[0]强制转换为字符串,但它总是不正确的。

function build_file_assoc() {
global $dir;
$assoc = [];
   $file_assoc = file($dir . 'rsnjxdlxwxwun.dbt'); 
   for($i = 0; $i < count($file_assoc) - 1; $i++) {
   if($i > 0) {
        list($parent_folder, $image_name, $tag, $size, $date) = explode("/", $file_assoc[$i]);
        $assoc[] = [
            'parent_folder' => (string)$parent_folder,
            'image_name' => $image_name,
            'tag' => $tag,
            'size' => $size,
            'date' => $date
        ];
       }
   }
   return $assoc;
}
$g = build_file_assoc();
$first = $g[0]['parent_folder'];
echo $first == "0000000"; // false

文件内容:

0000000/BirthstoneEnsemble/f/0/1380152724
0000000/Thumbs.db/a/83968/1384248954
0000000/bridal images for frame/f/0/1380152879

尝试打印数组print_r($pieces),您将看到exlode后在特定字段中保存的内容。

如果我根据你的问题运行代码,它会起作用:

<?php 
$line = '0000000/BirthstoneEnsemble/f/0/1380152724';
$pieces = explode("/", $line);
echo var_dump($pieces[0] == "0000000"); //true
 ?>

我会使用相同的比较运算符,并将castpieces[0]类型作为字符串。

$line = '0000000/BirthstoneEnsemble/f/0/1380152724';
$pieces = explode('/',$line);
//echo '<pre>',print_r($pieces),'</pre>';
if((string)$pieces[0]==='0000000'){
    echo true;
}else{
    echo false;
}
// output: 1

var_dump

检查片段的实际值,因为其中可能有空格或制表符-var_dump会为您显示。

类似问题

看看这篇文章。