Jump to content

Searching keywords from array


MuseiKaze

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.