Looking for the most optimal way to parse a complex search string and reduce it to have 1 Boolean operator between terms. The operators are OR, AND, NOR.
Ex:
1. OR AND NOR NOR AND AND Fred OR NOR AND Wilma NOR AND AND Barney OR âThe and AND Flintstonesâ OR AND NOR NOR AND AND
OR AND NOR nor AND AND Fred OR NOR AND Wilma NOR and AND Barney OR âThe and AND Flintstonesâ OR and NOR NOR AND AND (added Mar-05-2014)
Result:
Fred OR Wilma NOR Barney OR “The and AND Flintstones”
Note: I am looking for PHP code implementation.
Your best bet would be to read the input word-by-word and process it in a state machine.
Something along the lines:
While it might be possible to do this using regular expressions, it’d be complex and quite unmanageable .
Result: