1

I have looked as an example provided here How do I check if a string contains a specific word? and tried to implement the same logic in my code.

However, the results that I get is not what I would have expected.

My code looks following

foreach ($files as $file_name) {
        require_once "Classes/PHPExcel.php";
        $tmpfname = "./product_files/".$file_name;
        //echo $tmpfname;
        //die();
        $excelReader = PHPExcel_IOFactory::createReaderForFile($tmpfname);
        $excelObj = $excelReader->load($tmpfname);
        $worksheet = $excelObj->getSheet(0);
        $lastRow = $worksheet->getHighestRow();
        echo $file_name;
         if (strpos($file_name, 'aroundnoon' !== false)) {
                    echo 'Aroundnoon </br>' ;
                 }
         else if (strpos($file_name, 'classic_drinks' !== false)) {
            echo 'Classic Drinks </br>' ;
         }
         else 
         {
             echo 'This is stupid.. </br>';
         }
}

The file names are aroundnoon.xlsx and classic_drinks.xlsx however when I run this file, I get the following results Sample

Where am I going wrong?

Artur L
  • 37
  • 7

1 Answers1

-1

As mentioned in the comments, you have misplaced the ) in both of the strpos() calls. Check the code below :

$files = [

    'aroundnoon.xlsx',
    'classic_drinks.xlsx'
];

foreach ($files as $file_name) {
    if (strpos($file_name, 'aroundnoon') !== false) {
        echo 'Aroundnoon </br>' ;
    }
    else if (strpos($file_name, 'classic_drinks') !== false) {
        echo 'Classic Drinks </br>' ;
    }
    else {
        echo 'This is stupid.. </br>';
   }
}

Check the output here : https://ideone.com/zpW8Mg

Happy Coder
  • 4,255
  • 13
  • 75
  • 152