1

I'm putting together a search form. I want to provide the user the option of searching with logical operators. Examples of search queries:

  • red || ((green && pink))

  • !red && !green

The data which is being searched is stored in an array. I have a few ways I can provide this functionality by chopping the search string up but I'm wondering if anyone knows of any modules which I could utilize before is start coding something from scratch?

I should also mention that I'm aware of security issues surrounding techniques but I'm not worried about SQL injections etc. as this tool isn't going to be on a public facing server.

3
  • could you let the user search with regular expressions? Commented May 3, 2013 at 19:22
  • Just a caveat, code written for private servers has a tendency to later be copied and used for public servers without much scrutiny. People tend to cut and paste. You might preface it with a huge warning or, better yet, include code to avoid SQL injections. Commented May 3, 2013 at 19:23
  • Are you using MySQL? Its fulltext search feature supports boolean searching automatically. Commented May 3, 2013 at 19:25

2 Answers 2

2

Look into using a proper Lexer or PEG, such as php-peg or PHP_LexerGenerator, which allows you to generate code that parses and validates a grammar for such uses.

You might also want to look at tools like ANTLRWorks, helping debugging and creating grammars.

Sign up to request clarification or add additional context in comments.

1 Comment

Wow! I've been toying with PHP for years now and I've never even heard of these before! Thank you very much @fiskfisk. I'm having a play with PEG now. I shall share my results once I've completed the task.
0

Your question is a little vague but I think that eval() could be used to easily solve your problem, if you don't know it executes code that you give it as a string.

2 Comments

No, never use eval for something "the user provides ..". In fact, never use eval. Never. (if you don't know when you can break this rule, don't break it)
He just said he didn't care about security, I know eval is a terrible function to use but for his purposes it seemed like a good choice. If used with a function like filter it could actually be a very simple solution to his problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.