Forum Moderators: bakedjake
Now after I have exploded the filter in and filter out variables into arrays ($elements is the max number of search terms used), here's the code:
for ($i = 0; $i <= $elements; $i++) {
if (!(empty($search[$i])) ) {
$shell = ($shell." ¦ grep -i ".$search[$i]);
}
if (!(empty($notsearch[$i])) ) {
$shell = ($shell." ¦ grep -i -v ".$notsearch[$i]);
}
}
For the shell part, I've tried three different commands, the fastest at the top.
$searchresults = `head -[totalno. of lines] [path] $shell`;
$searchresults = `sed '' [path] $shell`;
$searchresults = `grep \ [path] $shell`;
Basically, what I need is a simple command that'll do nothing, so I can tack on the " ¦ grep -i $seachstring" search terms. I'm trying to keep the script as fast as possible, it is already slow.
Thanks.
cat will work, as mcavic mentioned, as long as you're directing the searched content to the standard input (STDIN) of the pipe, which you seem to be..
But you might not need the null command at all -- many shell commands default to reading from STDIN. grep, cat, sed all do. Commands that don't will usually take "-" as a file argument to read from STDIN.
If you can avoid the extra overhead, you should...but if you can't, cat is probably the best choice. It's small and very fast.
If the file I am searching was 100 lines, names "file.txt" in the root my server, this is what it'd look like if I searched to filter in 'one' 'two' 'three' and to filter out 'four'.
"head -100 /home/user/public_html/file.txt ¦ grep -i one ¦ grep -i two ¦ grep -i three ¦ grep -i -v four"
The way it's coded is to just add a pipe for each filter in or out term, in the form " ¦ grep -i [search term]".
I don't know enough about STDIN to do without the first pipe.
$file = "myfile.txt";
$fp = fopen("$file", "r");
# Read lines from the file
while ($line = fgets($fp)) {
$m = 0;
# If it matches any of these, then it's a match
foreach ($search as $s) {
if (preg_match("/$s/", $line)) {
$m = 1;
}
}
# If it matches any of these, then it's not
# a match, afterall
foreach ($notsearch as $s) {
if (preg_match("/$s/", $line)) {
$m = 0;
}
}
# Display it if it is a match
if ($m == 1) {
print "$line";
}
}
fclose($fp);
$file = "file.txt";
$fp = fopen("$file", "r");
while ($line = fgets($fp)) {
$m = 0;
foreach ($search as $s) {
if (preg_match("/$s/i", $line)) {
$m = 1;
}
}
foreach ($notsearch as $s) {
if (!(empty($s)) ) {
if (preg_match("/$s/i", $line)) {
$m = 0;
}
}
}
if ( $m == 1 ) {
$searchresults = $searchresults.$line;
}
}
fclose($fp);
I've never used MySQL before what kind of PHP code would you use for the fastest possible search for multiple terms?
Sorry this is now getting even more off-topic.