473,396 Members | 1,898 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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|apple|orange|banana|
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_db]</h4>");
$pages_found = " ";
foreach ($search_data as $search_page) {
$page_arr = explode("|", $search_page);
$found_count = 0;
$qry_array = split('[, ]+',trim(strtolower($query)));
foreach ($qry_array as $qry) {
if (in_array($qry, $page_arr)) {
++$found_count;
$pages_found .= $page_arr[0] . " ";
}
}
if ($found_count == count($qry_array)) $result_arr[] = $page_arr[0];
}
return $result_arr;
}

many thanks all.
Jul 16 '05 #1
2 2607

"fartsniff" <fa**@sniff.com> wrote in message
news:vh************@corp.supernews.com...
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|apple|orange|banana|
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_db]</h4>");
$pages_found = " ";
foreach ($search_data as $search_page) {
$page_arr = explode("|", $search_page);
$found_count = 0;
$qry_array = split('[, ]+',trim(strtolower($query)));
foreach ($qry_array as $qry) {
if (in_array($qry, $page_arr)) {
++$found_count;
$pages_found .= $page_arr[0] . " ";
}
}
if ($found_count == count($qry_array)) $result_arr[] = $page_arr[0];
}
return $result_arr;
}

many thanks all.


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?
Jul 16 '05 #2
> 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?


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.
Jul 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Sims | last post by:
Hi, Given a string $txt and an array of strings $txt_array what would be the best/fastest way to search in _insensitive_ case if $txt is in $text_array and, if it is, where is it? Because I...
3
by: Tommy Lang | last post by:
I am working on this project and I need some help/pointers/comments to get me started, I am stuck. The program will be used to store information in an array while it is running. I need to store...
3
by: tsunami | last post by:
hi all; I have an array and want to insert all the elements from this array to a binary search tree.That array is an object of the class of a stringtype which includes overloaded "< > = =="...
2
by: Carlos K | last post by:
Hello I'm having some difficulty searching a set of rows in a DataRow collection.. This is my question What would be an efficient way to search any column of an DataRow array Let me try to...
3
by: jac130 | last post by:
the program runs, and user is prompted via inputbox to enter an integer-this is the size of the array, then the user fills the array with that many values...but as the user enters the values, i need...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.