Question

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';
        }
    }
Was it helpful?

Solution

You need to trim the lines that are read from the file as they have linefeed characters on them.

I also stored all the details in the 'info' array...

    if ($handle = fopen(__DIR__ ."/description.txt", "r")) {
    while (($line = fgets($handle)) !== false) {

        $line = trim($line);
        // process each line and put title & desc into an info array
        $parts = explode('|', $line);

        $fileDetails = array();
        $fileDetails['filename'] = ! empty($parts[0]) ? $parts[0] : 'no-filename';
        $fileDetails['title'] = ! empty($parts[1]) ? $parts[1] : 'no-title';
        $fileDetails['description'] = ! empty($parts[2]) ? $parts[2] : 'no-description';

        $this->info[] = $fileDetails;
    }
    fclose($handle);

OTHER TIPS

Please look at size of array function.count

$parts = explode('|', $line);
$this->info[$parts[0]]['title'] = count($parts)==1 : 'no-title';
$this->info[$parts[0]]['description'] = count($parts)==2 : 'no-description';

check - it helps

$parts = explode('|', $line);

$no_title = 'no-title';
if($parts[1]){
  $no_title = $parts[1];
}

$no_description = 'no-description';

if($parts[2]){
  $no_description = $parts[2];
}

$this->info[$parts[0]]['title'] = $no_title;
$this->info[$parts[0]]['description'] = $no_description;

for example

$line =  'filename';

$no_title = 'no-title';
if($parts[1]){
  $no_title = $parts[1];
}

$no_description = 'no-description';

if($parts[2]){
  $no_description = $parts[2];
}

echo $no_title .' ';
echo $no_description;

Output:

no-title no-description 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top