Search code examples
phpstrstr

Using strstr() to exclude values in excel while not shifting cells up


I am working in PHP using strstr() to remove values which are extracted from a MYSQL db to be outputted to an excel file-

CODE:

        If (!strstr($value, '<script>')){
       $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $value);

           $row++;
        }

This code excludes the columns that contain a <script> tag. However it shifts the contents of the cells up like so:

=====================================
|Question          |  Answer        |
=====+========+===============+=====|
|   Male/female    |                |
|----+--------+---------------+-----|
|                  |      F         |
|----+--------+---------------+-----|

Instead I would like it to skip the rows like so:

=====================================
|Question          |  Answer        |
=====+========+===============+=====|
|                  |                |
|----+--------+---------------+-----|
|   Male/female    |      F         |
|----+--------+---------------+-----|

Solution

  • Your code is written so that the row is only incremented if your script tag is not found. However, it sounds like you want to unconditionally increment; if that's the case, then move $row++ to outside the if statement, like so:

    if (!strstr($value, '<script>')){
        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $value);
    }
    $row++;
    

    If this isn't what you were looking for, then please provide a sample input that can be matched to the output, as this would make what you need crystal clear.