Bee
Bee

Reputation: 309

find character in string from csv file using php?

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???

Upvotes: 0

Views: 561

Answers (3)

Subin Thomas
Subin Thomas

Reputation: 1416

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
?>

Upvotes: 0

Tomasz
Tomasz

Reputation: 5162

You can use strpos()

if ( strpos($outbound, 'NOT') !== false ) {
// "NOT" WORD FOUND IN STRING
}

Upvotes: 0

syck
syck

Reputation: 3029

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?

Upvotes: 1

Related Questions