MuseiKaze Posted January 28, 2010 Share Posted January 28, 2010 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 Quote Link to comment Share on other sites More sharing options...
teamatomic Posted January 28, 2010 Share Posted January 28, 2010 You start with line 1 of arr, which is your content look for your first search element, if its found its written to a file You increment the line and the search element. etc. when you run out of search elements your while ends and no matter how many lines are in your contents file you will only search up to the count of the search array. So the problem is that you are using the count of the wrong array. So try: while ($i <= count($arr)) $line = 1; while ($i <= count($search)) { if (strpos($arr[$line], $search[$i]) > 0) { $write = $arr[$line]; fwrite($fh, $write); } $line++; $i++; HTH Teamatomic Quote Link to comment Share on other sites More sharing options...
MuseiKaze Posted January 28, 2010 Author Share Posted January 28, 2010 Thanks for your reply, ive tried both methods for counting the array, but they either only search for the first keyword in the array $search[0] or produce no results. Its really got me confused, I know its probably something small, but I just cant figure out what. Quote Link to comment Share on other sites More sharing options...
teamatomic Posted January 29, 2010 Share Posted January 29, 2010 Given: $content_array $search_array $found_array=array(); foreach($content_array as $content){ trim("$content"); foreach($search_array as $term){ trim("$term"); if (strlen(strstr($content,$term))>0) {array_push($found_array,$content);} } } not tested but should be OK. HTH Teamatomic Quote Link to comment Share on other sites More sharing options...
MuseiKaze Posted January 29, 2010 Author Share Posted January 29, 2010 Just tested it out this morning. Had to fix a few minor things to make it loop for each term but it worked out great. Thanks heaps. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.