ok this is very strange, i tried over and over using different methods but nothin seems to work for what I need.
I have a description.txt
file with contents in form of:
filename|title|description
now, for each line of that file I do:
$parts = explode('|', $line);
$this->info[$parts[0]]['title'] = isset($parts[1]) ? $parts[1] : 'no-title';
$this->info[$parts[0]]['description'] = isset($parts[2]) ? $parts[2] : 'no-description';
however, because some of lines in my description.txt
file do not have title & description, but filename only
I thought that checking for existence of those missing parts will do, and in that case echo no-title
and no-description
respectively. But the code above does not yield those strings, and I don't see anything echoed out.
I tried with array_key_exists('1', $parts)
also, without success. Tried with is_null
without success.
I don't have any ideas why this doesn't work.
Just to add, code is working fine if none of the pipe delimited parts is missing, so it's just a problem of how to catch those non-existing.
Doing var_dump
on $parts[1]
, for lines that do not have anything after filename gives me null
This is output of var_dump($parts); die;
if first line is filename only:
array (size=1)
0 => string '10172608_1425353644382351_9063428062883749844_n.jpg
' (length=52)
Like for example on the first line:
filename
filename|title|description
I managed to make it work with !empty($parts[1])
check, but then I have to have in my description.txt
file:
filename|||
for it to work, and I would like to skip those extra pipes when I don't have title or description.
HERE IS THE ENTIRE LOOP:
This works if description.txt
file is like:
filename|||
for lines that do not contain title or description.
if($handle = @fopen($this->path . "/" . $this->desc_file, "r")) {
while (($line = fgets($handle)) !== false) {
// process each line and put title & desc into an info array
$parts = explode('|', $line);
$this->info[$parts[0]]['title'] = ! empty($parts[1]) ? $parts[1] : 'no-title';
$this->info[$parts[0]]['description'] = ! empty($parts[2]) ? $parts[2] : 'no-description';
}
}