0

I am really a newbie in php. I have a problem in doing this..

I have sample.csv file contains 3 rows: inbound(1st row), outbound(2nd row), and date(3rd row).

sample.csv

      **inbound**                   **outbound**            **date**       
  IN/15@001234                  OUT/000000163-000000as     1/12/2014
  IN/15@004323                  NOT/000000141-00000043     1/14/2014
  IN/15@005555                  OUT/000000164-000000jk     1/15/2014

is it possible to display the all columns where 2ndrow is start with "NOT" and a number before char "-" is 141???

output:

IN/15@004323                NOT/000000141-00000043     1/14/2014

i dont know if it is possible... please help me.. I have a code below. But it only open the csv file...

$file  = fopen('Master.csv', 'r');

echo "<table style='border: 2px solid black; text-align:left'>";
while (($line = fgetcsv($file)) !== FALSE) {  
    list($inbound, $outbound, $date) = $line;          

        echo "<tr>";
        echo "<td>$inbound</td>"; 
        echo"<td>$outbound</td>"; 
        echo "<td>$date</td>";
        echo "</tr>"; 

}
echo "</table>";

is it possible to display the all columns where 2ndrow is start with "NOT" and a number before char "-" is 141???

Bee
  • 309
  • 3
  • 14
  • You can convert the csv records into an array like so: http://stackoverflow.com/questions/7502370/converting-csv-to-array and then check if a string in the array contains a value like so: http://stackoverflow.com/questions/19445798/php-check-if-string-contains-a-value-in-array – nomistic Aug 26 '15 at 15:37

3 Answers3

1

Inserting

if (preg_match('/^NOT/', $outbound)) continue;

after the list()... statement should be sufficient.

But your data does not look like being comma-seperated, rather than tab-seperated. And perhaps you mean columns when talking about rows at the beginning?

syck
  • 2,984
  • 1
  • 13
  • 23
  • in NOT/000000141-00000043.. how to get the char before the "-" hypen?? i.e 141 – Bee Aug 26 '15 at 15:40
  • `preg_match('/\\d*-/', $outbound, $matches)` will give you all numbers before the hyphen in `$matches[0]`. – syck Aug 26 '15 at 15:43
  • I can show you how it works. You will have to write your code yourself. – syck Aug 26 '15 at 15:46
0

You can use strpos()

if ( strpos($outbound, 'NOT') !== false ) {
// "NOT" WORD FOUND IN STRING
}
Tomasz
  • 4,847
  • 2
  • 32
  • 41
0

Try this out. This will work with comma separated csv file.

echo "<table border = 1><tr><td>first</td><td>second</td><td>third</td></tr>"; //creating table
$handle = fopen('fe.csv', "r");     //open csv file 

while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)   //read csv file row by row
{
    //check both NOT and 141- in the string
    if ( (strpos($data[1], 'NOT') !== false ) && (strpos($data[1], '141-') !== false )) {
        //add required field data to table
        echo "<tr>";
        echo "<td>".$data[0]."</td>"; 
        echo"<td>".$data[1]."</td>"; 
        echo "<td>".$data[2]."</td>";
        echo "</tr>"; 
    }
}
echo "</table>"; //close table
?>
Kevin Brown-Silva
  • 40,873
  • 40
  • 203
  • 237
Subin Thomas
  • 1,408
  • 10
  • 19