Searching keywords from array

Posted on 16th Feb 2014 by admin

Hello everyone, Im just wondering if someone could help me with another issue im having. Im still learning PHP and I have been for a few months now, but every now and then I get stuck on small things. I hope to become a regular here, but I know I cannot contribute much at the moment while im still learning. Hopefully once I know more I will try to contribute more.

Anyways to my problem im having at the moment, im trying to search for a keyword, the keywords are stored in an array and also the strings that im searching through are exploded into an array for each line (each product is on a seperate line). Heres the main portion of the code which I need help with.

$contents = /* csv file which is being read */
$arr = explode("n",$contents);

$search = array
(
"ACCESS POINT",
"AUDIO EQUIPMENT",
"WEB CAMERA"
);

$result = "results.csv";
$fh = fopen($result, 'wa') or die("can't open file");

if ($search)
{
$i = 0;
$line = 1;
while ($i <= count($search))
{
if (strpos($arr[$line], $search[$i]) > 0)
{
$write = $arr[$line];
fwrite($fh, $write);
}
$line++;
$i++;
}
}

Ive altered it a bit to make it shorter but the part of it works when I have $search as a single keyword, however when I add the list of keywords into an array and count up using $search[$i] and $i++, it doesnt seem to search through the list of keywords for each line im searching through.

Any help or advice would be appriciated.

Thanks

Other forums