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

1

3 Answers 3

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?

Sign up to request clarification or add additional context in comments.

3 Comments

in NOT/000000141-00000043.. how to get the char before the "-" hypen?? i.e 141
preg_match('/\\d*-/', $outbound, $matches) will give you all numbers before the hyphen in $matches[0].
I can show you how it works. You will have to write your code yourself.
0

You can use strpos()

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

4 Comments

That would yield 0 which would be treated as false.
in NOT/000000141-00000043.. how to get the char before the "-" hypen?? i.e 141
@Bee what do you mean?
I want to display Which start with NOT and the 3 char before hypen(-) is 141...... then it shows NOT/000000141-00000043
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
?>

1 Comment

@Bee : I think I am using the correct string comparison which you required. Update if anything else.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.