chrisleon wrote:I have a text file, roughly 9000 records long.
Any reason why you're not using a DB such as MySQL to make all of this a LOT easier?
chrisleon wrote:delimited by a ~
Out of curiosity, what happens if your data has a literal '~' in it?
chrisleon wrote:I have a front end to this, an input box that you type a search into.
I'm assuming that $SearchBox is defined to have this value above the code snippet?
chrisleon wrote:the isset then allows the second preg match to work.
But how is it supposed to do that? As I said before, $stuff isn't defined in the code snippet you provided for us, so as far as we know the variable is guaranteed not to exist.
chrisleon wrote:I have never defined my variables before, so that is not the issue.
Er, what? So you're saying I can come up with some arbitrary variable name, let's say $broccoli, and PHP is supposed to know what value should be in $broccoli ?
If moving this flat file to a database such as MySQL (where it'd be a simple SELECT query and you'd have the results), then I'd say you need to start from scratch. I understand how you split the search term into different 'words', and I see how you're looping through each line of the file. Beyond that, your code doesn't make any sense.
In fact, you don't even need to use preg_match() at all - you don't have any regular expression patterns (unless you're allowing these in your search box, in which case you have other problems you need to prevent). Instead, inside your inner foreach() loop, you'd simply use [man]stripos/man:
if(stripos($piece[5], $searchpiece) !== FALSE)
echo "$pieces[0] $pieces[2] $pieces[3] $pieces[4] $pieces[5] $pieces[6]<br>";
EDIT: After looking at your code again, I believe one problem you have is that you think this:
$string = "'var1', 'var2'";
function($string);
is the same as this:
function('var1', 'var2');
when this is not the case. In the first code snippet, function is called with one argument, while in the second code snippet it is called with two. Using variables as arguments isn't like copying-and-pasting the contents of that value in the variable's location in the PHP script; the variable's value is sent to the function as a parameter.