Help searching through an array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • fartsniff

    Help searching through an array

    hello all,
    this is continued from a previous post. i am still stuck, and really could
    use some help.

    basically, via a form, search words are submitted, i.e. "apple". it then
    searches through
    the contents of the index_file and sees if a match is found.
    if you searched for "apple orange", then a match for index1.html (listed
    below) would be
    returned.

    however, if you search for "apple pie", NO match is returned. even though,
    "apple" and "pie"
    are in the file, they are just not on the same line.

    can someone please look at the function and see if it is possible to make
    searches return matches
    if two or more words are entered, and they exist on different lines ?

    the contents of $GLOBALS[index_file]:
    http://127.0.0.1/index1.html|app le|orange|banan a|
    http://127.0.0.1/index2.html|pie |

    we'll hardcode the variable:
    $query = "apple pie";

    the function:
    function s_search($query ) {
    $query = trim(strtolower (c_strip_chars( $query)));
    $search_data = @file($GLOBALS[index_file]) or die("<h4
    align=\"center\ ">$GLOBALS[err_no_search_d b]</h4>");
    $pages_found = " ";
    foreach ($search_data as $search_page) {
    $page_arr = explode("|", $search_page);
    $found_count = 0;
    $qry_array = split('[, ]+',trim(strtolo wer($query)));
    foreach ($qry_array as $qry) {
    if (in_array($qry, $page_arr)) {
    ++$found_count;
    $pages_found .= $page_arr[0] . " ";
    }
    }
    if ($found_count == count($qry_arra y)) $result_arr[] = $page_arr[0];
    }
    return $result_arr;
    }

    many thanks all.


  • Randell D.

    #2
    Re: Help searching through an array


    "fartsniff" <fart@sniff.com > wrote in message
    news:vh6509rd1g tr6d@corp.super news.com...[color=blue]
    > hello all,
    > this is continued from a previous post. i am still stuck, and really could
    > use some help.
    >
    > basically, via a form, search words are submitted, i.e. "apple". it then
    > searches through
    > the contents of the index_file and sees if a match is found.
    > if you searched for "apple orange", then a match for index1.html (listed
    > below) would be
    > returned.
    >
    > however, if you search for "apple pie", NO match is returned. even though,
    > "apple" and "pie"
    > are in the file, they are just not on the same line.
    >
    > can someone please look at the function and see if it is possible to make
    > searches return matches
    > if two or more words are entered, and they exist on different lines ?
    >
    > the contents of $GLOBALS[index_file]:
    > http://127.0.0.1/index1.html|app le|orange|banan a|
    > http://127.0.0.1/index2.html|pie |
    >
    > we'll hardcode the variable:
    > $query = "apple pie";
    >
    > the function:
    > function s_search($query ) {
    > $query = trim(strtolower (c_strip_chars( $query)));
    > $search_data = @file($GLOBALS[index_file]) or die("<h4
    > align=\"center\ ">$GLOBALS[err_no_search_d b]</h4>");
    > $pages_found = " ";
    > foreach ($search_data as $search_page) {
    > $page_arr = explode("|", $search_page);
    > $found_count = 0;
    > $qry_array = split('[, ]+',trim(strtolo wer($query)));
    > foreach ($qry_array as $qry) {
    > if (in_array($qry, $page_arr)) {
    > ++$found_count;
    > $pages_found .= $page_arr[0] . " ";
    > }
    > }
    > if ($found_count == count($qry_arra y)) $result_arr[] = $page_arr[0];
    > }
    > return $result_arr;
    > }
    >
    > many thanks all.
    >
    >[/color]

    Could you clarify that what you want to do is perform a 'AND' search - thus,
    if the user enters APPLE and ORANGE, and both these are in the one line,
    then your query should return true with something... however if APPLE and
    PIE is entered, and only APPLE but not PIE is on a single line, then it
    should return FALSE...

    Am I correct with that belief?

    If so I think you might need something like preg_match with a regular
    expression... Can you paste two or three lines of index_file so I can get a
    look at how you have it parsed? ie are its values comma or space seperated?


    Comment

    • fartsniff

      #3
      Re: Help searching through an array

      > Could you clarify that what you want to do is perform a 'AND' search -
      thus,[color=blue]
      > if the user enters APPLE and ORANGE, and both these are in the one line,
      > then your query should return true with something... however if APPLE and
      > PIE is entered, and only APPLE but not PIE is on a single line, then it
      > should return FALSE...
      >
      > Am I correct with that belief?
      >
      > If so I think you might need something like preg_match with a regular
      > expression... Can you paste two or three lines of index_file so I can get[/color]
      a[color=blue]
      > look at how you have it parsed? ie are its values comma or space[/color]
      seperated?[color=blue]
      >[/color]

      actually, if someone enters in APPLE AND ORANGE then it will return a
      "match"
      which displays index1.html, but if someone entered in APPLE AND PIE then it
      would return 2 "matches", displaying both index1.html and index2.html

      i understand what James says below about "exploding" the query into words,
      but
      am not sure how to completely implement it.

      trust me folks, i am trying to get this to work.


      Comment

      Working...