0

I have a line in a file like this:

0000000/BirthstoneEnsemble/f/0/1380152724

I explode by $pieces = explode("/", $line);

when I do echo $pieces[0] == "0000000" it returns false. I try to cast pieces[0] to string, but it is always incorrect.

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

file contents:

0000000/BirthstoneEnsemble/f/0/1380152724
0000000/Thumbs.db/a/83968/1384248954
0000000/bridal images for frame/f/0/1380152879
Stephen K
  • 697
  • 9
  • 26

4 Answers4

0

Try print array print_r($pieces) and you will see what is saved in specific fields after exlode.

Lukáš B.
  • 26
  • 5
0

If I run the code from your question, it works:

<?php 
$line = '0000000/BirthstoneEnsemble/f/0/1380152724';
$pieces = explode("/", $line);

echo var_dump($pieces[0] == "0000000"); //true
 ?>
Lasse
  • 1,414
  • 11
  • 19
  • the lines in the file are not wrapped in quotes.. your example makes the entire thing a string – Stephen K Dec 13 '13 at 21:24
  • @StephenKnoth Yes but I wrote it *before* you posted full source. Never the less, `file()` probably returns an array of strings. You can test this with `var_dump($first);` – Lasse Dec 13 '13 at 21:30
0

I would use the identical comparison operator and type cast pieces[0] as a string.

$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
Samuel Cook
  • 16,620
  • 7
  • 50
  • 62
0

var_dump

Check the actual value of your piece, since it's possible that you have a space or a tab character in there - var_dump will show it for you.

similar issue

Take a look at this post.

Community
  • 1
  • 1
M K
  • 9,138
  • 7
  • 43
  • 44